feat: cli subcommands to migrate and serve

This commit is contained in:
Aravinth Manivannan 2022-09-09 17:15:44 +05:30
parent 22e7bc29fa
commit 9877891a46
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
1 changed files with 28 additions and 1 deletions

View File

@ -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<ArcCtx>;
#[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}");