Skip to main content

doido_controller/
server.rs

1//! Convenience entry point for booting an application's HTTP server.
2
3use crate::config;
4use crate::stack::MiddlewareStack;
5
6/// Boots the HTTP server for `router`.
7///
8/// The listen address is the `server.bind` IP joined with `server.port` from
9/// `config/<env>.yml` (the environment comes from [`crate::Environment::get_env`]).
10/// When no config file is present the defaults `0.0.0.0:3000` are used.
11pub async fn start_server(router: axum::Router) -> std::io::Result<()> {
12    let config = config::load();
13    let server = config.server();
14    let addr = format!("{}:{}", server.bind, server.port);
15
16    // Apply the always-on middleware (request/response logging + panic
17    // recovery) so every request is traced through the global subscriber.
18    let router = MiddlewareStack::default().apply(router);
19
20    let listener = tokio::net::TcpListener::bind(&addr).await?;
21    tracing::info!("listening on http://{addr}");
22    tracing::info!("routes:\n{}", crate::route_table::format_routes());
23    axum::serve(listener, router).await
24}