hello world
This commit is contained in:
96
src/main.rs
Normal file
96
src/main.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
use askama::Template;
|
||||
use axum::{
|
||||
Router,
|
||||
body::Bytes,
|
||||
extract::{self, MatchedPath},
|
||||
http::{HeaderMap, Request, StatusCode},
|
||||
response::{Html, IntoResponse, Response},
|
||||
routing::get,
|
||||
};
|
||||
use tokio::time::Duration;
|
||||
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
|
||||
use tracing::{Span, info_span};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
let app = app();
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
||||
let _ = axum::serve(listener, app).await;
|
||||
}
|
||||
|
||||
fn app() -> Router {
|
||||
Router::new().route("/greet/{name}", get(greet)).layer(
|
||||
TraceLayer::new_for_http()
|
||||
.make_span_with(|request: &Request<_>| {
|
||||
let matched_path = request
|
||||
.extensions()
|
||||
.get::<MatchedPath>()
|
||||
.map(MatchedPath::as_str);
|
||||
let path = &request.uri().to_string();
|
||||
|
||||
info_span!(
|
||||
"http_request",
|
||||
method = ?request.method(),
|
||||
matched_path,
|
||||
path,
|
||||
some_other_field = tracing::field::Empty,
|
||||
)
|
||||
})
|
||||
.on_request(|_request: &Request<_>, _span: &Span| {
|
||||
tracing::debug!("started processing request")
|
||||
})
|
||||
.on_response(|_response: &Response, _latency: Duration, _span: &Span| {
|
||||
tracing::debug!("finished processing request")
|
||||
})
|
||||
.on_body_chunk(|_chunk: &Bytes, _latency: Duration, _span: &Span| {
|
||||
tracing::debug!("sending body chunk")
|
||||
})
|
||||
.on_eos(
|
||||
|_trailers: Option<&HeaderMap>, _stream_duration: Duration, _span: &Span| {
|
||||
tracing::debug!("stream closed")
|
||||
},
|
||||
)
|
||||
.on_failure(
|
||||
|_error: ServerErrorsFailureClass, _latency: Duration, _span: &Span| {
|
||||
tracing::error!("something went wrong")
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
async fn greet(extract::Path(name): extract::Path<String>) -> impl IntoResponse {
|
||||
let template = HelloTemplate { name };
|
||||
HtmlTemplate(template)
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "hello.html")]
|
||||
struct HelloTemplate {
|
||||
name: String,
|
||||
}
|
||||
|
||||
struct HtmlTemplate<T>(T);
|
||||
impl<T> IntoResponse for HtmlTemplate<T>
|
||||
where
|
||||
T: Template,
|
||||
{
|
||||
fn into_response(self) -> Response {
|
||||
match self.0.render() {
|
||||
Ok(html) => Html(html).into_response(),
|
||||
Err(err) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Failed to render template. Error: {err}"),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user