use crate::web::MiddlewareConnections; use clap::{CommandFactory, Parser, Subcommand}; use clap_complete::Shell; use sea_orm::Database; use sea_orm_migration::prelude::*; #[derive(Parser, Clone)] #[clap(author, version, about, long_about = None)] struct Opts { /// Name of the person to greet #[clap(short, long = "database", value_parser, env)] database_url: String, #[clap(short, long, value_parser, env, default_value = "0.0.0.0")] address: String, #[clap(short, long, value_parser, env)] redis_url: Option, #[clap(short, long, value_parser, env, default_value = "8888")] port: u16, #[clap(subcommand)] commands: Option, } #[derive(Subcommand, Clone)] enum Commands { /// Print shell completion scripts on standard output Completion { #[clap(arg_enum)] shell: Option, }, /// Migrate database up and down Migrate { #[clap(subcommand)] direction: MigrationDirection, }, } #[derive(Subcommand, Clone)] enum MigrationDirection { /// Migrate database up Up { steps: Option }, /// Do a fresh install Fresh, /// Remove previous tables and do a fresh install Refresh, } mod completion; mod migrations; mod web; #[actix_web::main] async fn main() -> Result<(), Box> { let args = Opts::parse(); env_logger::init(); if let Some(command) = args.commands.clone() { match command { Commands::Completion { shell } => { let mut command = Opts::command(); completion::handle(shell, &mut command); std::process::exit(0); } _ => {} } } let database = Database::connect(args.clone().database_url).await?; let redis = match args.clone().redis_url { Some(redis_url) => Some(redis::Client::open(redis_url)?), None => None, }; if let Some(command) = args.commands.clone() { match command { Commands::Migrate { direction } => match direction { MigrationDirection::Up { steps } => { migrations::Migrator::up(&database, steps).await? } MigrationDirection::Fresh => migrations::Migrator::fresh(&database).await?, MigrationDirection::Refresh => migrations::Migrator::refresh(&database).await?, }, _ => (), }; return Ok(()); } web::run(MiddlewareConnections { database, redis }, args).await }