add basic backend
This commit is contained in:
@ -1,3 +1,79 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use std::sync::mpsc;
|
||||
|
||||
use clap::Parser;
|
||||
use nuchat::app;
|
||||
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,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let args = Args::parse();
|
||||
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 listener = TcpListener::bind(format!("{}:{}", args.host, args.port))
|
||||
.await
|
||||
.unwrap();
|
||||
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
||||
|
||||
let (app, rx) = app();
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(shutdown_signal(rx))
|
||||
.await
|
||||
.unwrap();
|
||||
info!("Server stopped");
|
||||
}
|
||||
|
||||
#[allow(clippy::unused_async)]
|
||||
#[allow(clippy::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...");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user