From e534c87eef07c0ff2ab5378f24dc208058784c21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=97=8D+85CD?= <50108258+kwaa@users.noreply.github.com> Date: Thu, 17 Oct 2024 19:26:06 +0800 Subject: [PATCH] feat!: use clap (#77) * feat!: use clap * fix(commands): optional * chore(main): remove pub Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * chore(run): use question mark --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- Cargo.lock | 1 + Cargo.toml | 2 ++ src/main.rs | 93 +++++++++++++++-------------------------------------- src/run.rs | 71 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 67 deletions(-) create mode 100644 src/run.rs diff --git a/Cargo.lock b/Cargo.lock index a97ec81f..da5d21c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1324,6 +1324,7 @@ name = "hatsu" version = "0.3.0-beta.7" dependencies = [ "activitypub_federation", + "clap", "dotenvy", "hatsu_api_apub", "hatsu_apub", diff --git a/Cargo.toml b/Cargo.toml index 32e9f249..41e81875 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -96,6 +96,7 @@ axum = { version = "0.7", features = ["tokio", "macros"] } # axum-extra = { version = "0.8", features = ["query", "typed-routing"] } base64-simd = "0.8" chrono = "0.4" +clap = "4.5" enum_delegate = "0.2" feed-rs = "2.1" futures = "0.3" @@ -150,6 +151,7 @@ hatsu_tracing = { workspace = true } hatsu_utils = { workspace = true } human-panic = "2" activitypub_federation = { workspace = true } +clap = { workspace = true } dotenvy = "0.15" sea-orm = { workspace = true } tokio = { workspace = true } diff --git a/src/main.rs b/src/main.rs index 787a3544..8d06787d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,80 +2,39 @@ #[global_allocator] static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc; -use std::{env, ops::Deref, path::Path}; - -use activitypub_federation::config::FederationConfig; -use hatsu_apub::actors::ApubUser; -use hatsu_db_migration::{Migrator, MigratorTrait}; -use hatsu_db_schema::prelude::User; -use hatsu_utils::{AppData, AppEnv, AppError}; +use clap::{Parser, Subcommand}; +use hatsu_utils::AppError; use human_panic::{metadata, setup_panic}; -use sea_orm::{ActiveModelTrait, Database, EntityTrait, IntoActiveModel}; + +mod run; + +#[derive(Debug, Parser)] +#[command( + name = "hatsu", + version = hatsu_utils::VERSION, + about, +)] +struct Args { + #[command(subcommand)] + command: Option, +} + +#[derive(Debug, Subcommand)] +enum Commands { + Run, +} #[tokio::main] async fn main() -> Result<(), AppError> { setup_panic!(metadata!().homepage("https://github.com/importantimport/hatsu/issues")); - hatsu_tracing::init()?; - - tracing::info!("{}", AppEnv::info()); + let args = Args::parse(); - tracing::info!("loading environment variables"); - if dotenvy::dotenv().is_err() { - let env_file = - env::var("HATSU_ENV_FILE").unwrap_or_else(|_| String::from("/etc/hatsu/.dev")); - if dotenvy::from_path(Path::new(&env_file)).is_err() { - tracing::debug!("no .env file found"); + if let Some(command) = args.command { + match command { + Commands::Run => run::run().await, } + } else { + run::run().await } - - let env = AppEnv::init()?; - - tracing::info!("connecting database: {}", &env.hatsu_database_url); - let conn = Database::connect(&env.hatsu_database_url) - .await - .expect("database connection failed"); - - tracing::info!("running database migration"); - Migrator::up(&conn, None).await?; - - tracing::info!("checking primary account: {}", &env.hatsu_primary_account); - let primary_account: ApubUser = match User::find_by_id( - hatsu_utils::url::generate_user_url(&env.hatsu_domain, &env.hatsu_primary_account)? - .to_string(), - ) - .one(&conn) - .await? - { - Some(db_user) => db_user.into(), - None => ApubUser::new(&env.hatsu_domain, &env.hatsu_primary_account) - .await? - .deref() - .clone() - .into_active_model() - .insert(&conn) - .await? - .into(), - }; - - let data = AppData { - conn, - env: env.clone(), - }; - - tracing::info!("setup federation config"); - let federation_config = FederationConfig::builder() - .domain(&env.hatsu_domain) - .signed_fetch_actor(&primary_account) - .app_data(data) - // TODO: - // Disable this configuration when Pleroma supports HTTP Signature draft-11 - // 当 Pleroma 支持 HTTP Signature draft-11 时,禁用此配置 - // https://git.pleroma.social/pleroma/pleroma/-/issues/2939 - .http_signature_compat(true) - .build() - .await?; - - tracing::info!("starting backend"); - hatsu_backend::run(federation_config).await } diff --git a/src/run.rs b/src/run.rs new file mode 100644 index 00000000..56c24a79 --- /dev/null +++ b/src/run.rs @@ -0,0 +1,71 @@ +use std::{env, ops::Deref, path::Path}; + +use activitypub_federation::config::FederationConfig; +use hatsu_apub::actors::ApubUser; +use hatsu_db_migration::{Migrator, MigratorTrait}; +use hatsu_db_schema::prelude::User; +use hatsu_utils::{AppData, AppEnv, AppError}; +use sea_orm::{ActiveModelTrait, Database, EntityTrait, IntoActiveModel}; + +pub async fn run() -> Result<(), AppError> { + hatsu_tracing::init()?; + + tracing::info!("{}", AppEnv::info()); + + tracing::info!("loading environment variables"); + if dotenvy::dotenv().is_err() { + let env_file = + env::var("HATSU_ENV_FILE").unwrap_or_else(|_| String::from("/etc/hatsu/.dev")); + if dotenvy::from_path(Path::new(&env_file)).is_err() { + tracing::debug!("no .env file found"); + } + } + + let env = AppEnv::init()?; + + tracing::info!("connecting database: {}", &env.hatsu_database_url); + let conn = Database::connect(&env.hatsu_database_url).await?; + + tracing::info!("running database migration"); + Migrator::up(&conn, None).await?; + + tracing::info!("checking primary account: {}", &env.hatsu_primary_account); + let primary_account: ApubUser = match User::find_by_id( + hatsu_utils::url::generate_user_url(&env.hatsu_domain, &env.hatsu_primary_account)? + .to_string(), + ) + .one(&conn) + .await? + { + Some(db_user) => db_user.into(), + None => ApubUser::new(&env.hatsu_domain, &env.hatsu_primary_account) + .await? + .deref() + .clone() + .into_active_model() + .insert(&conn) + .await? + .into(), + }; + + let data = AppData { + conn, + env: env.clone(), + }; + + tracing::info!("setup federation config"); + let federation_config = FederationConfig::builder() + .domain(&env.hatsu_domain) + .signed_fetch_actor(&primary_account) + .app_data(data) + // TODO: + // Disable this configuration when Pleroma supports HTTP Signature draft-11 + // 当 Pleroma 支持 HTTP Signature draft-11 时,禁用此配置 + // https://git.pleroma.social/pleroma/pleroma/-/issues/2939 + .http_signature_compat(true) + .build() + .await?; + + tracing::info!("starting backend"); + hatsu_backend::run(federation_config).await +}