put router definition into own function

This commit is contained in:
Khaïs COLIN 2025-10-26 13:41:42 +01:00
parent 897309a3b9
commit 425ec50a5f
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo

View file

@ -167,6 +167,36 @@ fn do_migrations(conn: &mut Connection) -> rusqlite::Result<()> {
Ok(())
}
fn app(conn: Connection) -> axum::Router {
Router::new()
.route("/", get(root))
.route("/increase/{id}", post(increase))
.route("/decrease/{id}", post(decrease))
.route("/set/{id}/to/{amount}", post(set))
.layer(
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
let matched_path = request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str);
let request_id = request
.extensions()
.get::<RequestId>()
.map(ToString::to_string)
.unwrap_or_else(|| "unknown".into());
info_span!(
"request",
method = ?request.method(),
matched_path,
uri = ?request.uri(),
id = %request_id,
)
}),
)
.layer(RequestIdLayer)
.with_state(Arc::new(Mutex::new(conn)))
}
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
tracing_subscriber::registry()
@ -197,33 +227,7 @@ async fn main() -> Result<(), std::io::Error> {
PreparedStatements::check(&conn).expect("failed to prepare sql statements");
let app = Router::new()
.route("/", get(root))
.route("/increase/{id}", post(increase))
.route("/decrease/{id}", post(decrease))
.route("/set/{id}/to/{amount}", post(set))
.layer(
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
let matched_path = request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str);
let request_id = request
.extensions()
.get::<RequestId>()
.map(ToString::to_string)
.unwrap_or_else(|| "unknown".into());
info_span!(
"request",
method = ?request.method(),
matched_path,
uri = ?request.uri(),
id = %request_id,
)
}),
)
.layer(RequestIdLayer)
.with_state(Arc::new(Mutex::new(conn)));
let app = app(conn);
let address = "0.0.0.0:3001";
let listener = tokio::net::TcpListener::bind(address)