diff options
| author | rtkay123 <dev@kanjala.com> | 2025-11-23 10:31:25 +0200 |
|---|---|---|
| committer | rtkay123 <dev@kanjala.com> | 2025-11-23 10:31:25 +0200 |
| commit | f9af6bf27fb448a64fdfeb219e4d2302938c5a1a (patch) | |
| tree | fd163ce9fba96d73550b33d5bdda4abbd2aadbfc /crates | |
| parent | 7f65b47bfb333632c87927c38b90918bedcf5eae (diff) | |
| download | sellershut-f9af6bf27fb448a64fdfeb219e4d2302938c5a1a.tar.bz2 sellershut-f9af6bf27fb448a64fdfeb219e4d2302938c5a1a.zip | |
feat: start server
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/sellershut/Cargo.toml | 7 | ||||
| -rw-r--r-- | crates/sellershut/src/main.rs | 68 |
2 files changed, 73 insertions, 2 deletions
diff --git a/crates/sellershut/Cargo.toml b/crates/sellershut/Cargo.toml index e964cf7..1726be8 100644 --- a/crates/sellershut/Cargo.toml +++ b/crates/sellershut/Cargo.toml @@ -8,3 +8,10 @@ documentation.workspace = true description.workspace = true [dependencies] +anyhow = "1.0.100" +axum = { version = "0.8.7", features = ["macros"] } +config = { version = "0.15.19", default-features = false, features = ["toml", "convert_case"] } +tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread", "signal"] } +tower-http = { version = "0.6.6", features = ["trace", "timeout"] } +tracing.workspace = true +tracing-subscriber = { version = "0.3.20", features = ["env-filter"] } diff --git a/crates/sellershut/src/main.rs b/crates/sellershut/src/main.rs index e7a11a9..22dcb86 100644 --- a/crates/sellershut/src/main.rs +++ b/crates/sellershut/src/main.rs @@ -1,3 +1,67 @@ -fn main() { - println!("Hello, world!"); +use std::time::Duration; + +use axum::{Router, routing::get}; +use tokio::{net::TcpListener, signal}; +use tower_http::{timeout::TimeoutLayer, trace::TraceLayer}; +use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; + +#[tokio::main] +async fn main()-> anyhow::Result<()> { + tracing_subscriber::registry() + .with( + tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| { + format!( + "{}=debug,tower_http=debug,axum=trace", + env!("CARGO_CRATE_NAME") + ) + .into() + }), + ) + .with(tracing_subscriber::fmt::layer().without_time()) + .init(); + + // Create a regular axum app. + let app = Router::new() + .route("/slow", get(|| tokio::time::sleep(Duration::from_secs(5)))) + .route("/forever", get(std::future::pending::<()>)) + .layer(( + TraceLayer::new_for_http(), + // Graceful shutdown will wait for outstanding requests to complete. Add a timeout so + // requests don't hang forever. + TimeoutLayer::new(Duration::from_secs(10)), + )); + + // Create a `TcpListener` using tokio. + let listener = TcpListener::bind("0.0.0.0:3000").await?; + + // Run the server with graceful shutdown + axum::serve(listener, app) + .with_graceful_shutdown(shutdown_signal()) + .await?; + + Ok(()) +} + +async fn shutdown_signal() { + 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 => {}, + } } |
