diff --git a/src/main.rs b/src/main.rs index 72dac82..4862b9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,13 +19,14 @@ use std::env; use actix_web::http::StatusCode; use actix_web::web::JsonConfig; use actix_web::{error::InternalError, middleware, App, HttpServer}; +use clap::{Parser, Subcommand}; use log::info; use lazy_static::lazy_static; mod api; mod ctx; -//mod db; +mod db; //mod docs; #[cfg(not(tarpaulin_include))] mod errors; @@ -71,6 +72,22 @@ pub const CACHE_AGE: u32 = 604800; use ctx::ArcCtx; pub type AppCtx = actix_web::web::Data; +#[derive(Parser)] +#[clap(author, version, about, long_about = None)] +struct Cli { + #[clap(subcommand)] + command: Commands, +} + +#[derive(Subcommand)] +enum Commands { + /// run database migrations + Migrate, + + /// run server + Serve, +} + #[actix_web::main] #[cfg(not(tarpaulin_include))] async fn main() -> std::io::Result<()> { @@ -78,6 +95,8 @@ async fn main() -> std::io::Result<()> { pretty_env_logger::init(); + let cli = Cli::parse(); + info!( "{}: {}.\nFor more information, see: {}\nBuild info:\nVersion: {} commit: {}", PKG_NAME, PKG_DESCRIPTION, PKG_HOMEPAGE, VERSION, GIT_COMMIT_HASH @@ -87,6 +106,14 @@ async fn main() -> std::io::Result<()> { let ctx = Ctx::new(&settings).await; let ctx = actix_web::web::Data::new(ctx); + match &cli.command { + Commands::Migrate => ctx.db.migrate().await.unwrap(), + Commands::Serve => serve(settings, ctx).await.unwrap(), + } + Ok(()) +} + +async fn serve(settings: Settings, ctx: AppCtx) -> std::io::Result<()> { let ip = settings.server.get_ip(); println!("Starting server on: http://{ip}");