-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat-cannon
- Loading branch information
Showing
11 changed files
with
182 additions
and
32 deletions.
There are no files selected for viewing
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
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
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
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,93 @@ | ||
use axum::{ | ||
async_trait, | ||
extract::{FromRequestParts, Request}, | ||
http::{request::Parts, Method, StatusCode, Uri}, | ||
middleware::Next, | ||
response::Response, | ||
}; | ||
use chrono::{DateTime, Utc}; | ||
use serde::Serialize; | ||
use serde_json::{json, Value}; | ||
use tracing::debug; | ||
use uuid::Uuid; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ReqStamp { | ||
pub uuid: Uuid, | ||
pub time_in: DateTime<Utc>, | ||
} | ||
|
||
pub async fn req_stamp(mut req: Request, next: Next) -> Response { | ||
let time_in = Utc::now(); | ||
let uuid = Uuid::new_v4(); | ||
|
||
req.extensions_mut().insert(ReqStamp { uuid, time_in }); | ||
|
||
next.run(req).await | ||
} | ||
|
||
#[async_trait] | ||
impl<S: Send + Sync> FromRequestParts<S> for ReqStamp { | ||
// TODO replace with our own error type that implements IntoResponse | ||
type Rejection = StatusCode; | ||
|
||
async fn from_request_parts( | ||
parts: &mut Parts, | ||
_state: &S, | ||
) -> core::result::Result<Self, Self::Rejection> { | ||
parts | ||
.extensions | ||
.get::<ReqStamp>() | ||
.cloned() | ||
.ok_or(StatusCode::INTERNAL_SERVER_ERROR) | ||
} | ||
} | ||
|
||
#[derive(Serialize)] | ||
struct RequestLogLine { | ||
/// Unique request identifier. | ||
uuid: String, | ||
/// Timestamp(rfc3339) of the log line. | ||
timestamp: String, | ||
/// Timestamp(rfc3339) of the request. | ||
time_in: String, | ||
/// Duration of the request in milliseconds. | ||
duration_ms: i64, | ||
|
||
/// HTTP path of the request. | ||
http_path: String, | ||
/// HTTP method of the request. | ||
http_method: String, | ||
|
||
// TODO: error handling | ||
/// The error variant. | ||
error_type: Option<String>, | ||
/// The error data. | ||
error_data: Option<Value>, | ||
} | ||
|
||
pub async fn log_request(uri: Uri, method: Method, req_stamp: ReqStamp, res: Response) -> Response { | ||
// TODO: grab error data from response | ||
// something like: | ||
// res.extensions_mut().get::<OurErrorType>(); | ||
|
||
let ReqStamp { uuid, time_in } = req_stamp; | ||
let now = Utc::now(); | ||
let duration = now - time_in; | ||
|
||
let log_line = RequestLogLine { | ||
uuid: uuid.to_string(), | ||
timestamp: now.to_rfc3339(), | ||
time_in: time_in.to_rfc3339(), | ||
duration_ms: duration.num_milliseconds(), | ||
http_path: uri.to_string(), | ||
http_method: method.to_string(), | ||
error_type: None, | ||
error_data: None, | ||
}; | ||
|
||
// TODO: send to logging services | ||
debug!("REQUEST LOG LINE:\n{}", json!(log_line)); | ||
|
||
res | ||
} |
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
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