refactor admin router into separate file
also set admin secret using command line args
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
use std::sync::mpsc;
|
||||
|
||||
use clap::Parser;
|
||||
use nuchat::Config;
|
||||
use nuchat::app;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::signal;
|
||||
@ -17,11 +18,16 @@ struct Args {
|
||||
/// Host to run server on
|
||||
#[arg(long, default_value = "127.0.0.1")]
|
||||
host: String,
|
||||
|
||||
/// Admin secret to use, leave blank to disable
|
||||
#[arg(long)]
|
||||
admin_secret: Option<String>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let args = Args::parse();
|
||||
let config = BinConfig::from_args(args);
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
|
||||
@ -31,12 +37,12 @@ async fn main() {
|
||||
.with(tracing_subscriber::fmt::layer().with_target(false))
|
||||
.init();
|
||||
|
||||
let listener = TcpListener::bind(format!("{}:{}", args.host, args.port))
|
||||
let listener = TcpListener::bind(format!("{}:{}", config.0.host, config.0.port))
|
||||
.await
|
||||
.unwrap();
|
||||
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
||||
|
||||
let (app, rx) = app();
|
||||
let (app, rx) = app(&config.0);
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(shutdown_signal(rx))
|
||||
.await
|
||||
@ -77,3 +83,14 @@ async fn shutdown_signal(rx: mpsc::Receiver<bool>) {
|
||||
}
|
||||
info!("Shutting server down gracefully...");
|
||||
}
|
||||
|
||||
struct BinConfig(Config);
|
||||
impl BinConfig {
|
||||
fn from_args(args: Args) -> Self {
|
||||
Self(Config {
|
||||
port: args.port,
|
||||
host: args.host,
|
||||
admin_secret: args.admin_secret,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user