add basic backend
This commit is contained in:
3
backend/src/lib.rs
Normal file
3
backend/src/lib.rs
Normal file
@ -0,0 +1,3 @@
|
||||
mod router;
|
||||
|
||||
pub use router::app;
|
||||
@ -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...");
|
||||
}
|
||||
|
||||
83
backend/src/router.rs
Normal file
83
backend/src/router.rs
Normal file
@ -0,0 +1,83 @@
|
||||
mod healthcheck;
|
||||
use std::sync::mpsc;
|
||||
use std::time::Duration;
|
||||
|
||||
use axum::extract::Request;
|
||||
use axum::middleware::{Next, from_fn};
|
||||
use axum::response::Response;
|
||||
use axum::routing::{get, post};
|
||||
use axum::{Router, body::Body};
|
||||
use http::StatusCode;
|
||||
use tower::ServiceBuilder;
|
||||
use tower_http::timeout::TimeoutLayer;
|
||||
use tower_http::trace::TraceLayer;
|
||||
use tracing::Level;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn app() -> (Router, mpsc::Receiver<bool>) {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
(
|
||||
Router::new()
|
||||
.route("/healthcheck", get(healthcheck::healthcheck))
|
||||
.route("/forever", get(std::future::pending::<()>))
|
||||
.nest("/admin", admin(tx))
|
||||
.layer(
|
||||
ServiceBuilder::new()
|
||||
.layer(
|
||||
TraceLayer::new_for_http().make_span_with(|req: &Request<Body>| {
|
||||
tracing::span!(
|
||||
Level::DEBUG,
|
||||
"request",
|
||||
trace_id = Uuid::now_v7().to_string(),
|
||||
method = format!("{}", req.method()),
|
||||
uri = format!("{}", req.uri()),
|
||||
)
|
||||
}),
|
||||
)
|
||||
.layer(TimeoutLayer::new(Duration::from_secs(10))),
|
||||
),
|
||||
rx,
|
||||
)
|
||||
}
|
||||
|
||||
fn admin(tx: mpsc::Sender<bool>) -> Router {
|
||||
let r = Router::new().route("/test", get(async || StatusCode::OK));
|
||||
|
||||
let r = add_shutdown_endpoint(r, tx);
|
||||
r.layer(from_fn(async |req: Request, next: Next| {
|
||||
if let Ok(secret) = std::env::var("ADMIN_SECRET") {
|
||||
println!("ADMIN_SECRET: {secret}");
|
||||
match req.headers().get("Authorization") {
|
||||
Some(key) if secret == key.to_owned() => (),
|
||||
_ => {
|
||||
return Response::builder()
|
||||
.status(StatusCode::UNAUTHORIZED)
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
next.run(req).await
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(feature = "shutdown")]
|
||||
fn add_shutdown_endpoint(r: Router, tx: mpsc::Sender<bool>) -> Router {
|
||||
r.route(
|
||||
"/shutdown",
|
||||
post(async move || {
|
||||
let res = tx.send(true);
|
||||
if res.is_ok() {
|
||||
StatusCode::OK
|
||||
} else {
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "shutdown"))]
|
||||
fn add_shutdown_endpoint(r: Router, _: mpsc::Sender<bool>) -> Router {
|
||||
r
|
||||
}
|
||||
6
backend/src/router/healthcheck.rs
Normal file
6
backend/src/router/healthcheck.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use axum::response::Json;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub async fn healthcheck() -> Json<Value> {
|
||||
Json(json!({"healthy": true}))
|
||||
}
|
||||
Reference in New Issue
Block a user