moving the create_onwer code to its own function to clean up main a bit

This commit is contained in:
shockrah 2021-01-23 18:18:01 -08:00
parent 49c675b97a
commit 84c865e194

View File

@ -38,7 +38,7 @@ mod perms;
mod messages;
mod admin;
mod http_params;
mod http;
mod testing;
const NO_ERR: u16 = 0;
@ -85,7 +85,7 @@ async fn main_responder(request: Request<Body>) -> Result<Response<Body>, hyper:
let method = parts.method;
let path = parts.uri.path();
let params_res = http_params::parse_params(&mut body).await;
let params_res = http::parse_json_params(&mut body).await;
if let Ok(params) = params_res {
let mysql_pool = Pool::new(&env::var("DATABASE_URL").unwrap());
@ -131,6 +131,38 @@ async fn start_server(ecode: u16) -> u16 {
}
async fn attempt_owner_creation(name: &str) {
/*
* Attempts to create an owner level account 'name' as the name
* Writes succesful output to stdout
* Writes error output to stderr
* NOTE: Messy because there's 0 other places where this kind of direct
* functionality is required. db-lib is basically built to talk to the api
* */
let p = Pool::new(&env::var("DATABASE_URL").unwrap());
let owner_secret = auth::generate_secret();
if let Ok(enc_secret) = auth::encrypt_secret(&owner_secret) {
if let Ok(response) = db::member::Member::add(&p, name, &enc_secret, perms::OWNER).await {
match response {
db::Response::Row(mut owner) => {
owner.secret = owner_secret; // giving the secret itself back to the user
println!("{}", serde_json::to_string(&owner).expect("SQL query passed but serde couldn't parse the data for some reason"))
},
db::Response::Empty => {
eprintln!("SQL server failed to return owner data, check configs and also the members table to make sure there's nothing there by accident");
},
_ => {}
};
}
else {
eprintln!("Could not communicate with the SQL server, check your configs!");
}
}
else {
eprintln!("Could not generate a proper secret");
}
p.disconnect();
}
#[tokio::main]
async fn main() -> Result<(), u16>{
@ -190,34 +222,7 @@ OPTIONS:
}
if let Some(owner_name) = args.value_of("create-owner") {
let p = Pool::new(&env::var("DATABASE_URL").unwrap());
let owner_secret = auth::generate_secret();
// creation of owner should just dump straight to stdout since this fires
// from a commandline parameter anyway
if let Ok(enc_secret) = auth::encrypt_secret(&owner_secret) {
match db::member::Member::add(&p, owner_name, &enc_secret, perms::OWNER).await {
Ok(response) => {
match response {
db::Response::Row(mut owner) => {
owner.secret = owner_secret; // giving the secret itself back to the user
println!("{}", serde_json::to_string(&owner).expect("SQL query passed but serde couldn't parse the data for some reason"))
},
db::Response::Empty => {
eprintln!("SQL server failed to return owner data, check configs and also the members table to make sure there's nothing there by accident");
},
_ => {}
};
},
Err(_) => {
eprintln!("Could not communicate with the SQL server, check your configs!");
}
}
}
else {
eprintln!("Could not generate a proper secret");
}
//println!("{}", serde_json::to_string(&owner).unwrap());
p.disconnect();
attempt_owner_creation(owner_name).await;
}
if args.is_present("server") {