summaryrefslogtreecommitdiff
path: root/server/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/main.rs')
-rw-r--r--server/src/main.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/server/src/main.rs b/server/src/main.rs
new file mode 100644
index 0000000..810e202
--- /dev/null
+++ b/server/src/main.rs
@@ -0,0 +1,66 @@
+mod errors;
+mod logger;
+
+use axum::{
+ http::{header, Request},
+ routing::get,
+ Json, Router,
+};
+use serde::Serialize;
+use std::time::Duration;
+use tower_http::sensitive_headers::SetSensitiveHeadersLayer;
+use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
+use tracing::Span;
+
+#[tokio::main]
+async fn main() {
+ let app = create_app().await;
+
+ let addr: String =
+ std::env::var("ALLOWED_HOST").unwrap_or_else(|_| "localhost:3000".to_string());
+ tracing::info!("Listening on {}", addr);
+
+ axum::Server::bind(&"127.0.0.1:3000".parse().unwrap())
+ .serve(app.into_make_service())
+ .await
+ .unwrap();
+}
+
+async fn create_app() -> Router {
+ logger::setup();
+
+ Router::new()
+ .route("/", get(hej))
+ // Mark the `Authorization` request header as sensitive so it doesn't
+ // show in logs.
+ .layer(SetSensitiveHeadersLayer::new(std::iter::once(
+ header::AUTHORIZATION,
+ )))
+ .layer(
+ TraceLayer::new_for_http()
+ .on_request(|request: &Request<_>, _span: &Span| {
+ tracing::info!("{} {}", request.method(), request.uri());
+ })
+ .on_failure(
+ |error: ServerErrorsFailureClass, latency: Duration, _span: &Span| {
+ tracing::error!("{} | {} s", error, latency.as_secs());
+ },
+ ),
+ )
+}
+
+// Example root which says hi
+async fn hej() -> Result<Json<Hej>, errors::Error> {
+ Ok(Json(Hej::new("hej verden".to_string())))
+}
+
+#[derive(Debug, Serialize)]
+struct Hej {
+ hello: String,
+}
+
+impl Hej {
+ fn new(hello: String) -> Self {
+ Self { hello }
+ }
+}