ptth/crates/ptth_server/src/bin/ptth_server.rs

79 lines
1.7 KiB
Rust

#![warn (clippy::pedantic)]
use std::{
path::PathBuf,
};
use structopt::StructOpt;
use tokio::runtime;
use ptth_server::{
load_toml,
run_server,
};
#[derive (Debug, StructOpt)]
struct Opt {
#[structopt (long)]
file_server_root: Option <PathBuf>,
#[structopt (long)]
asset_root: Option <PathBuf>,
#[structopt (long)]
config_path: Option <PathBuf>,
#[structopt (long)]
name: Option <String>,
#[structopt (long)]
print_tripcode: bool,
#[structopt (long)]
relay_url: Option <String>,
}
#[derive (Default, serde::Deserialize)]
pub struct ConfigFile {
pub name: Option <String>,
pub api_key: String,
pub relay_url: Option <String>,
pub file_server_root: Option <PathBuf>,
}
fn main () -> Result <(), anyhow::Error> {
tracing_subscriber::fmt::init ();
let opt = Opt::from_args ();
let asset_root = opt.asset_root;
let path = opt.config_path.clone ().unwrap_or_else (|| PathBuf::from ("./config/ptth_server.toml"));
let config_file: ConfigFile = load_toml::load (&path)?;
let config_file = ptth_server::ConfigFile {
name: opt.name.or (config_file.name).expect ("`name` must be provided in command line or config file"),
api_key: config_file.api_key,
relay_url: opt.relay_url.or (config_file.relay_url).expect ("`relay_url` must be provided in command line of config file"),
file_server_root: opt.file_server_root.or (config_file.file_server_root),
};
if opt.print_tripcode {
println! (r#"name = "{}""#, config_file.name);
println! (r#"tripcode = "{}""#, config_file.tripcode ());
return Ok (());
}
let rt = runtime::Runtime::new ()?;
rt.block_on (async move {
run_server (
config_file,
ptth_core::graceful_shutdown::init (),
Some (path),
asset_root
).await
})?;
Ok (())
}