119 lines
3.1 KiB
Rust
119 lines
3.1 KiB
Rust
use std::sync::mpsc;
|
|
|
|
use clap::Parser;
|
|
use nuchat::AppState;
|
|
use nuchat::Config;
|
|
use nuchat::NuState;
|
|
use nuchat::app;
|
|
use sqlx::Pool;
|
|
use sqlx::Postgres;
|
|
use tokio::net::TcpListener;
|
|
use tokio::signal;
|
|
use tracing::info;
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
|
|
|
#[derive(Parser, Debug)]
|
|
#[command(version, about, long_about = None)]
|
|
struct Args {
|
|
/// Port to run server on
|
|
#[arg(long, default_value_t = 7000)]
|
|
port: u32,
|
|
|
|
/// 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>,
|
|
|
|
/// postgres base url, should container users and host info
|
|
#[arg(long, default_value = "postgres://postgres:postgres@localhost:5432/")]
|
|
postgres_url: String,
|
|
|
|
/// name of database to use
|
|
#[arg(long, default_value = "nuchat_dev")]
|
|
database: 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(|_| {
|
|
format!("{}=debug,tower_http=debug", env!("CARGO_CRATE_NAME")).into()
|
|
}),
|
|
)
|
|
.with(tracing_subscriber::fmt::layer().with_target(false))
|
|
.init();
|
|
|
|
let pool = Pool::<Postgres>::connect(&config.0.postgres_url)
|
|
.await
|
|
.expect("Could not connect to database");
|
|
|
|
let listener = TcpListener::bind(format!("{}:{}", config.0.host, config.0.port))
|
|
.await
|
|
.unwrap();
|
|
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
|
|
|
let state = AppState::new(NuState::new(pool.clone(), config.0));
|
|
let (app, rx) = app(&state);
|
|
axum::serve(listener, app.with_state(state))
|
|
.with_graceful_shutdown(shutdown_signal(rx))
|
|
.await
|
|
.unwrap();
|
|
|
|
pool.close().await;
|
|
|
|
info!("Server stopped");
|
|
}
|
|
|
|
#[allow(clippy::unused_async)]
|
|
#[allow(unused)]
|
|
async fn await_shutdown(rx: mpsc::Receiver<bool>) -> Result<bool, mpsc::RecvError> {
|
|
rx.recv()
|
|
}
|
|
|
|
async fn shutdown_signal(rx: mpsc::Receiver<bool>) {
|
|
let endpoint = tokio::spawn(async move { rx.recv() }).into_future();
|
|
|
|
let ctrl_c = async {
|
|
signal::ctrl_c()
|
|
.await
|
|
.expect("failed to install Ctrl+C handler");
|
|
};
|
|
|
|
#[cfg(unix)]
|
|
let terminate = async {
|
|
signal::unix::signal(signal::unix::SignalKind::terminate())
|
|
.expect("failed to install signal handler")
|
|
.recv()
|
|
.await;
|
|
};
|
|
|
|
#[cfg(not(unix))]
|
|
let terminate = std::future::pending::<()>();
|
|
|
|
tokio::select! {
|
|
() = ctrl_c => {},
|
|
() = terminate => {},
|
|
_ = endpoint => {},
|
|
}
|
|
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,
|
|
postgres_url: args.postgres_url,
|
|
database_name: args.database,
|
|
})
|
|
}
|
|
}
|