diff --git a/src/main.rs b/src/main.rs index fc5bca16..7fea7a23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,4 @@ use std::env; -use std::io::{stdin, stdout, Write}; -use std::process::exit; use std::time::Duration; use actix_identity::IdentityMiddleware; @@ -11,14 +9,13 @@ use actix_web_static_files::ResourceFiles; use dotenv::dotenv; use sqlx::postgres::PgPool; -use crate::auth::utils::generate_salt_and_hash_plain_password; -use crate::models::User; use crate::postgres_session_store::SqlxPostgresqlSessionStore; +use crate::utils::manage_commands::{handle_command, parse_args}; mod auth; mod endpoints; -mod models; mod middleware; +mod models; mod utils; mod filters; @@ -27,95 +24,17 @@ mod postgres_session_store; include!(concat!(env!("OUT_DIR"), "/generated.rs")); include!(concat!(env!("OUT_DIR"), "/built.rs")); -pub enum Command { - Migrate, - CreateAdmin, -} - -pub struct Args { - command: Option, -} - -fn parse_args() -> Result { - let mut pargs = pico_args::Arguments::from_env(); - - let command = pargs.free_from_str::(); - - let mut args = Args { command: None }; - - if let Ok(parsed) = command { - match parsed.trim() { - "migrate" => args.command = Some(Command::Migrate), - "createadmin" => args.command = Some(Command::CreateAdmin), - _ => (), - } - } - - Ok(args) -} - -fn prompt(prompt: &str) -> anyhow::Result { - print!("{}: ", prompt); - stdout().flush()?; - - let mut input = String::new(); - stdin().read_line(&mut input)?; - - Ok(input.trim().to_string()) -} - #[actix_web::main] async fn main() -> anyhow::Result<()> { dotenv()?; - - let args = match parse_args() { - Ok(v) => v, - Err(err) => { - eprintln!("Error: {err}"); - std::process::exit(1); - } - }; + let args = parse_args()?; let pool = PgPool::connect(&env::var("DATABASE_URL")?).await?; let mailer = utils::email::get_mailer()?; let secret_key = Key::from(env::var("SECRET_KEY")?.as_bytes()); let store = SqlxPostgresqlSessionStore::from_pool(pool.clone().into()); - match args.command { - Some(Command::Migrate) => { - sqlx::migrate!("./migrations").run(&pool).await?; - - exit(0); - } - Some(Command::CreateAdmin) => { - let name = prompt("Full name of Admin")?; - let email = prompt("E-Mail of Admin (for login)")?; - let password = prompt("Password of Admin")?; - let retyped_password = prompt("Retype Passsword of Admin")?; - - if password != retyped_password { - eprintln!("Given passwords don't match!"); - exit(1) - } - - let (hash, salt) = generate_salt_and_hash_plain_password(&password)?; - - User::create( - &pool, - &name, - &email, - &hash, - &salt, - models::Role::Admin, - models::Function::Wachhabender, - 1, - ) - .await?; - - exit(0); - } - None => (), - }; + handle_command(args.command, &pool).await?; println!("Starting server on http://localhost:8080."); diff --git a/src/utils/manage_commands.rs b/src/utils/manage_commands.rs new file mode 100644 index 00000000..dcf940fc --- /dev/null +++ b/src/utils/manage_commands.rs @@ -0,0 +1,88 @@ +use std::{ + io::{stdin, stdout, Write}, + process::exit, +}; + +use sqlx::{Pool, Postgres}; + +use crate::{ + auth::utils::generate_salt_and_hash_plain_password, + models::{Function, Role, User}, +}; + +pub enum Command { + Migrate, + CreateAdmin, +} + +pub struct Args { + pub command: Option, +} + +pub fn parse_args() -> Result { + let mut pargs = pico_args::Arguments::from_env(); + + let command = pargs.free_from_str::(); + + let mut args = Args { command: None }; + + if let Ok(parsed) = command { + match parsed.trim() { + "migrate" => args.command = Some(Command::Migrate), + "createadmin" => args.command = Some(Command::CreateAdmin), + _ => (), + } + } + + Ok(args) +} + +fn prompt(prompt: &str) -> anyhow::Result { + print!("{}: ", prompt); + stdout().flush()?; + + let mut input = String::new(); + stdin().read_line(&mut input)?; + + Ok(input.trim().to_string()) +} + +pub async fn handle_command(command: Option, pool: &Pool) -> anyhow::Result<()> { + match command { + Some(Command::Migrate) => { + sqlx::migrate!("./migrations").run(pool).await?; + + exit(0); + } + Some(Command::CreateAdmin) => { + let name = prompt("Full name of Admin")?; + let email = prompt("E-Mail of Admin (for login)")?; + let password = prompt("Password of Admin")?; + let retyped_password = prompt("Retype Passsword of Admin")?; + + if password != retyped_password { + eprintln!("Given passwords don't match!"); + exit(1) + } + + let (hash, salt) = generate_salt_and_hash_plain_password(&password)?; + + User::create( + &pool, + &name, + &email, + &hash, + &salt, + Role::Admin, + Function::Wachhabender, + 1, + ) + .await?; + + exit(0); + } + None => (), + }; + + Ok(()) +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index aa5f45d4..158bdcdd 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1 +1,2 @@ pub mod email; +pub mod manage_commands;