-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract a run_server function into cja
- Loading branch information
Showing
7 changed files
with
151 additions
and
59 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,36 @@ | ||
use axum::{extract::Request, response::Response, serve::IncomingStream}; | ||
use miette::{Context, IntoDiagnostic}; | ||
use std::{convert::Infallible, net::SocketAddr}; | ||
use tokio::net::TcpListener; | ||
use tower_cookies::CookieManagerLayer; | ||
use tower_service::Service; | ||
|
||
pub mod cookies; | ||
pub mod session; | ||
|
||
pub mod trace; | ||
|
||
pub async fn run_server<AS: Clone + Sync + Send, S>(routes: axum::Router<AS>) -> miette::Result<()> | ||
where | ||
axum::Router<AS>: | ||
for<'a> Service<IncomingStream<'a>, Error = Infallible, Response = S> + Send + 'static, | ||
for<'a> <axum::Router<AS> as Service<IncomingStream<'a>>>::Future: Send, | ||
S: Service<Request, Response = Response, Error = Infallible> + Clone + Send + 'static, | ||
S::Future: Send, | ||
{ | ||
let tracer = trace::Tracer; | ||
let trace_layer = tower_http::trace::TraceLayer::new_for_http() | ||
.make_span_with(tracer) | ||
.on_response(tracer); | ||
|
||
let app = routes.layer(trace_layer).layer(CookieManagerLayer::new()); | ||
|
||
let addr = SocketAddr::from(([0, 0, 0, 0], 3000)); | ||
let listener = TcpListener::bind(&addr).await.unwrap(); | ||
tracing::debug!("listening on {}", addr); | ||
|
||
axum::serve(listener, app) | ||
.await | ||
.into_diagnostic() | ||
.wrap_err("Failed to run server") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use tower_http::trace::{MakeSpan, OnResponse}; | ||
use tracing::Level; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub(crate) struct Tracer; | ||
|
||
impl<Body> MakeSpan<Body> for Tracer { | ||
fn make_span(&mut self, request: &http::Request<Body>) -> tracing::Span { | ||
let route = http_route(request); | ||
let span_name = format!("{} {}", request.method(), route); | ||
|
||
tracing::span!( | ||
Level::INFO, | ||
"server.request", | ||
otel.name = span_name, | ||
kind = "server", | ||
uri = %request.uri(), | ||
url.path = %request.uri().path(), | ||
url.query = request.uri().query(), | ||
url.scheme = request.uri().scheme_str(), | ||
server.address = request.uri().host(), | ||
server.port = request.uri().port_u16(), | ||
http_version = ?request.version(), | ||
user_agent.original = request.headers().get("user-agent").and_then(|h| h.to_str().ok()), | ||
http.route = route, | ||
http.request.method = %request.method(), | ||
http.request.header.host = request.headers().get("host").and_then(|h| h.to_str().ok()), | ||
http.request.header.forwarded_for = request.headers().get("x-forwarded-for").and_then(|h| h.to_str().ok()), | ||
http.request.header.forwarded_proto = request.headers().get("x-forwarded-proto").and_then(|h| h.to_str().ok()), | ||
http.request.header.host = request.headers().get("x-forwarded-ssl").and_then(|h| h.to_str().ok()), | ||
http.request.header.referer = request.headers().get("referer").and_then(|h| h.to_str().ok()), | ||
http.request.header.fly_forwarded_port = request.headers().get("fly-forwarded-port").and_then(|h| h.to_str().ok()), | ||
http.request.header.fly_region = request.headers().get("fly-region").and_then(|h| h.to_str().ok()), | ||
http.request.header.via = request.headers().get("via").and_then(|h| h.to_str().ok()), | ||
|
||
http.response.status_code = tracing::field::Empty, | ||
http.response.header.content_type = tracing::field::Empty, | ||
) | ||
} | ||
} | ||
|
||
impl<Body> OnResponse<Body> for Tracer { | ||
fn on_response( | ||
self, | ||
response: &http::Response<Body>, | ||
latency: std::time::Duration, | ||
span: &tracing::Span, | ||
) { | ||
let status_code = response.status().as_u16(); | ||
tracing::event!( | ||
Level::INFO, | ||
status = status_code, | ||
latency = format_args!("{} ms", latency.as_millis()), | ||
"finished processing request" | ||
); | ||
|
||
span.record("http.response.status_code", status_code); | ||
span.record( | ||
"http.response.header.content_type", | ||
response | ||
.headers() | ||
.get("content-type") | ||
.and_then(|h| h.to_str().ok()), | ||
); | ||
} | ||
} | ||
|
||
#[inline] | ||
fn http_route<B>(req: &http::Request<B>) -> &str { | ||
req.extensions() | ||
.get::<axum::extract::MatchedPath>() | ||
.map_or_else(|| "", |mp| mp.as_str()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters