refactor: cleanup main.rs
This commit is contained in:
parent
99458f9056
commit
b220edf508
89
src/main.rs
89
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<Command>,
|
||||
}
|
||||
|
||||
fn parse_args() -> Result<Args, pico_args::Error> {
|
||||
let mut pargs = pico_args::Arguments::from_env();
|
||||
|
||||
let command = pargs.free_from_str::<String>();
|
||||
|
||||
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<String> {
|
||||
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.");
|
||||
|
||||
|
88
src/utils/manage_commands.rs
Normal file
88
src/utils/manage_commands.rs
Normal file
@ -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<Command>,
|
||||
}
|
||||
|
||||
pub fn parse_args() -> Result<Args, pico_args::Error> {
|
||||
let mut pargs = pico_args::Arguments::from_env();
|
||||
|
||||
let command = pargs.free_from_str::<String>();
|
||||
|
||||
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<String> {
|
||||
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<Command>, pool: &Pool<Postgres>) -> 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(())
|
||||
}
|
@ -1 +1,2 @@
|
||||
pub mod email;
|
||||
pub mod manage_commands;
|
||||
|
Loading…
x
Reference in New Issue
Block a user