feat: cli subcommands to migrate and serve
This commit is contained in:
parent
22e7bc29fa
commit
9877891a46
1 changed files with 28 additions and 1 deletions
29
src/main.rs
29
src/main.rs
|
@ -19,13 +19,14 @@ use std::env;
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use actix_web::web::JsonConfig;
|
use actix_web::web::JsonConfig;
|
||||||
use actix_web::{error::InternalError, middleware, App, HttpServer};
|
use actix_web::{error::InternalError, middleware, App, HttpServer};
|
||||||
|
use clap::{Parser, Subcommand};
|
||||||
use log::info;
|
use log::info;
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
mod ctx;
|
mod ctx;
|
||||||
//mod db;
|
mod db;
|
||||||
//mod docs;
|
//mod docs;
|
||||||
#[cfg(not(tarpaulin_include))]
|
#[cfg(not(tarpaulin_include))]
|
||||||
mod errors;
|
mod errors;
|
||||||
|
@ -71,6 +72,22 @@ pub const CACHE_AGE: u32 = 604800;
|
||||||
use ctx::ArcCtx;
|
use ctx::ArcCtx;
|
||||||
pub type AppCtx = actix_web::web::Data<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]
|
#[actix_web::main]
|
||||||
#[cfg(not(tarpaulin_include))]
|
#[cfg(not(tarpaulin_include))]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
|
@ -78,6 +95,8 @@ async fn main() -> std::io::Result<()> {
|
||||||
|
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
|
|
||||||
|
let cli = Cli::parse();
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"{}: {}.\nFor more information, see: {}\nBuild info:\nVersion: {} commit: {}",
|
"{}: {}.\nFor more information, see: {}\nBuild info:\nVersion: {} commit: {}",
|
||||||
PKG_NAME, PKG_DESCRIPTION, PKG_HOMEPAGE, VERSION, GIT_COMMIT_HASH
|
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 = Ctx::new(&settings).await;
|
||||||
let ctx = actix_web::web::Data::new(ctx);
|
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();
|
let ip = settings.server.get_ip();
|
||||||
println!("Starting server on: http://{ip}");
|
println!("Starting server on: http://{ip}");
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue