refactor: into crates

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-04-30 20:36:45 +02:00
parent 14ef235dd9
commit 1be3a4fa6c
5 changed files with 200 additions and 172 deletions

View File

@@ -0,0 +1,42 @@
use std::{net::SocketAddr, sync::Arc};
use axum::{extract::MatchedPath, http::Request, routing::get, Router};
use tower_http::trace::TraceLayer;
use crate::state::{self, SharedState, State};
async fn root() -> &'static str {
"Hello, hyperlog!"
}
pub async fn serve(host: SocketAddr) -> 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);
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();
Ok(())
}