Improvements
This commit is contained in:
31
src/lib.rs
Normal file
31
src/lib.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use std::{io, time::Duration};
|
||||
|
||||
use axum::{routing, Router};
|
||||
use tokio::net::TcpListener;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tower_http::{timeout::TimeoutLayer, trace::TraceLayer};
|
||||
|
||||
pub struct Config {
|
||||
pub listen_port: u16,
|
||||
}
|
||||
|
||||
pub async fn run(config: Config, token: CancellationToken) -> Result<(), io::Error> {
|
||||
let app = Router::new()
|
||||
.route("/", routing::get(|| async { "Hello world!" }))
|
||||
.layer((
|
||||
TraceLayer::new_for_http(),
|
||||
TimeoutLayer::new(Duration::from_secs(10)),
|
||||
));
|
||||
|
||||
let listener = TcpListener::bind(("0.0.0.0", config.listen_port))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let _ = axum::serve(listener, app)
|
||||
.with_graceful_shutdown(async move {
|
||||
token.cancelled().await;
|
||||
})
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user