diff options
author | Cara Salter <cara@devcara.com> | 2022-04-27 13:56:24 -0400 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-04-27 13:56:24 -0400 |
commit | be5f240ca895001314a4474c6a26e7f4e704c57f (patch) | |
tree | 2ae214fa5a612b90740a03b53204fccb86de65c8 /src/main.rs | |
download | solard-be5f240ca895001314a4474c6a26e7f4e704c57f.tar.gz solard-be5f240ca895001314a4474c6a26e7f4e704c57f.zip |
Flake deployment
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..5777fa6 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,56 @@ +use axum::{ + error_handling::HandleErrorLayer, + http::StatusCode, + response::IntoResponse, + routing::get, + Json, Router +}; + +use serde::{Deserialize, Serialize}; +use std::{net::SocketAddr, time::Duration}; + +use tower::{BoxError, ServiceBuilder}; +use tower_http::trace::TraceLayer; + +use tracing_subscriber::prelude::*; + +#[tokio::main] +async fn main() { + tracing_subscriber::registry() + .with(tracing_subscriber::EnvFilter::new( + std::env::var("RUST_LOG") + .unwrap_or_else(|_| "waifud=info,tower_http=debug".into()), + )) + .with(tracing_subscriber::fmt::layer()) + .init(); + + let app = Router::new() + .route("/health", get(health_check)) + .layer( ServiceBuilder::new() + .layer(HandleErrorLayer::new(|error: BoxError| async move { + if error.is::<tower::timeout::error::Elapsed>() { + Ok(StatusCode::REQUEST_TIMEOUT) + } else { + Err(( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Unhandled internal error: {}", error), + )) + } + })) + .timeout(Duration::from_secs(10)) + .layer(TraceLayer::new_for_http()) + .into_inner(), + ); + + let addr = SocketAddr::from(([127,0,0,1], 3000)); + tracing::info!("Listening on {}", addr); + + axum::Server::bind(&addr) + .serve(app.into_make_service()) + .await + .unwrap(); +} + +async fn health_check() -> &'static str { + "OK" +} |