feat: add docs

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-08-10 00:17:12 +02:00
parent a5196b005e
commit cf321d110e
4 changed files with 563 additions and 496 deletions

View File

@@ -13,6 +13,13 @@ dotenv.workspace = true
axum.workspace = true
serde = { version = "1.0.197", features = ["derive"] }
sqlx = { version = "0.7.3", features = ["runtime-tokio", "tls-rustls", "postgres", "uuid", "time"] }
sqlx = { version = "0.7.3", features = [
"runtime-tokio",
"tls-rustls",
"postgres",
"uuid",
"time",
] }
uuid = { version = "1.7.0", features = ["v4"] }
tower-http = { version = "0.5.2", features = ["cors", "trace"] }
mad = { git = "https://github.com/kjuulh/mad", branch = "main" }

View File

@@ -3,9 +3,10 @@ use std::{net::SocketAddr, ops::Deref, sync::Arc};
use anyhow::Context;
use axum::extract::MatchedPath;
use axum::http::Request;
use axum::Router;
use axum::routing::get;
use axum::Router;
use clap::{Parser, Subcommand};
use mad::{Mad, MadError};
use sqlx::{Pool, Postgres};
use tower_http::trace::TraceLayer;
@@ -36,32 +37,42 @@ async fn main() -> anyhow::Result<()> {
let state = SharedState(Arc::new(State::new().await?));
let app = Router::new()
.route("/", get(root))
.with_state(state.clone())
.layer(
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
// Log the matched route's path (with placeholders not filled in).
// Use request.uri() or OriginalUri if you want the real path.
let matched_path = request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str);
let state = state.clone();
Mad::builder()
.add_fn(move |_cancel| {
let state = state.clone();
async move {
let app = Router::new()
.route("/", get(root))
.with_state(state.clone())
.layer(TraceLayer::new_for_http().make_span_with(
|request: &Request<_>| {
let matched_path = request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str);
tracing::info_span!(
"http_request",
method = ?request.method(),
matched_path,
some_other_field = tracing::field::Empty,
)
}), // ...
);
tracing::info_span!(
"http_request",
method = ?request.method(),
matched_path,
some_other_field = tracing::field::Empty,
)
},
));
tracing::info!("listening on {}", host);
let listener = tokio::net::TcpListener::bind(host).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
tracing::info!("listening on {}", host);
let listener = tokio::net::TcpListener::bind(host).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.context("axum server stopped")
.map_err(MadError::Inner)?;
Ok(())
}
})
.run()
.await?;
}
Ok(())
@@ -103,4 +114,3 @@ impl State {
Ok(Self { db })
}
}