Skip to content

Commit

Permalink
feat!: use clap (#77)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
kwaa and coderabbitai[bot] authored Oct 17, 2024
1 parent 85204a0 commit e534c87
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 67 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 }
Expand Down
93 changes: 26 additions & 67 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Commands>,
}

#[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
}
71 changes: 71 additions & 0 deletions src/run.rs
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e534c87

Please sign in to comment.