mod cli; use clap::Parser; use cli::{ Channels, Cli, Commands, DaemonAction, MailAction, ServiceAction, WechatAction, WorkerAction, }; use ias_daemon::supervisor::{ print_logs, restart_service, start_service, status_service, stop_service, }; use ias_db::connect_db; use ias_service::{delete_account, init_logging, list_accounts, login_user, rename_account}; #[tokio::main] async fn main() -> anyhow::Result<()> { dotenvy::dotenv().ok(); init_logging(); let cli = Cli::parse(); match cli.command { Commands::Service { command } => { let action = command.unwrap_or(ServiceAction::Run); match action { ServiceAction::Run => { ias_daemon::run_daemon().await?; } ServiceAction::Start => start_service().await?, ServiceAction::Stop => stop_service().await?, ServiceAction::Restart => restart_service().await?, ServiceAction::Status => status_service().await?, ServiceAction::Logs { lines } => print_logs(lines)?, } } Commands::Daemon { command } => match command { DaemonAction::Run => ias_daemon::run_daemon().await?, }, Commands::Worker { command } => { let pool = connect_db().await?; match command { WorkerAction::Assistant => ias_assistant::run_worker(pool).await?, WorkerAction::Wechat { account_ids } => { ias_channel::run_wechat_worker(pool, account_ids).await? } WorkerAction::Mail => ias_channel::run_mail_worker(pool).await?, WorkerAction::Function { name } => ias_function::run_worker(name).await?, WorkerAction::Scheduler => ias_scheduler::run_worker(pool).await?, WorkerAction::Logging => ias_logging::run_worker(pool).await?, } } Commands::Http { host } => { let pool = connect_db().await?; run_http(pool, host).await?; } Commands::Channel { command } => { let pool = connect_db().await?; match command { Channels::Wechat { command } => match command { WechatAction::Login => login_user(&pool).await, WechatAction::List => list_accounts(&pool).await, WechatAction::Delete { account } => delete_account(&pool, account).await, WechatAction::Rename { account, name } => { rename_account(&pool, account, name).await } }, } } Commands::Mail { command } => { let pool = connect_db().await?; match command { MailAction::Auth => ias_mail::auth::auth_account(&pool).await?, MailAction::List => ias_mail::auth::list_accounts(&pool).await?, MailAction::Show { id } => ias_mail::auth::show_account(&pool, id).await?, MailAction::Edit { id } => ias_mail::auth::edit_account(&pool, id).await?, MailAction::Settings { id } => { ias_mail::auth::configure_account_settings(&pool, id).await? } MailAction::Check { id } => ias_mail::auth::check_account(&pool, id).await?, MailAction::Pull { id } => ias_mail::auth::pull_mail(&pool, id).await?, MailAction::Delete { id } => ias_mail::auth::delete_account(&pool, id).await?, MailAction::Enable { id } => { ias_mail::auth::enable_account(&pool, id, true).await? } MailAction::Disable { id } => { ias_mail::auth::enable_account(&pool, id, false).await? } MailAction::Messages { page, per_page } => { ias_mail::auth::list_messages(&pool, page, per_page).await? } MailAction::Message { id } => ias_mail::auth::show_message(&pool, id).await?, } } } Ok(()) } async fn run_http(pool: sqlx::PgPool, host: String) -> anyhow::Result<()> { let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false); tokio::spawn(async move { let _ = tokio::signal::ctrl_c().await; let _ = shutdown_tx.send(true); }); let routes = ias_service::extra_http_routes(pool.clone()); ias_http::create_http(host, shutdown_rx, pool, routes).await }