mirror of
https://github.com/DefectingCat/phthonus
synced 2025-07-15 16:41:32 +00:00
refactor(axum): remove useless middlewares
This commit is contained in:
@ -6,8 +6,11 @@ Web framework comparison in my stack.
|
||||
|
||||
- Read environment variables from `.env` file.
|
||||
- Log request info to std.
|
||||
- Loggin request http method, host, uri, and UA
|
||||
- Loggin response status code, latency
|
||||
- Serialize json and plain text.
|
||||
- JWT support.
|
||||
- Graceful shutdown.
|
||||
|
||||
## Frameworks
|
||||
|
||||
|
24
web/Rust/axum/src/routes/json.rs
Normal file
24
web/Rust/axum/src/routes/json.rs
Normal file
@ -0,0 +1,24 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use axum::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::error::ErrorCode;
|
||||
|
||||
use super::{RouteResponse, RouteResult};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct JsonData {
|
||||
pub name: Cow<'static, str>,
|
||||
}
|
||||
|
||||
pub async fn json() -> RouteResult<JsonData> {
|
||||
let data = JsonData { name: "xfy".into() };
|
||||
let res = RouteResponse {
|
||||
code: ErrorCode::Normal,
|
||||
message: None,
|
||||
data,
|
||||
};
|
||||
|
||||
Ok(Json(res))
|
||||
}
|
@ -11,7 +11,7 @@ use axum::{
|
||||
};
|
||||
use serde::Serialize;
|
||||
use tower::ServiceBuilder;
|
||||
use tower_http::{compression::CompressionLayer, cors::CorsLayer, timeout::TimeoutLayer};
|
||||
use tower_http::timeout::TimeoutLayer;
|
||||
use tracing::info;
|
||||
|
||||
use crate::{
|
||||
@ -19,6 +19,8 @@ use crate::{
|
||||
middlewares::{add_version, logging_route},
|
||||
};
|
||||
|
||||
pub mod json;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct RouteResponse<T>
|
||||
where
|
||||
@ -34,12 +36,11 @@ pub type RouteResult<T> = AppResult<Json<RouteResponse<T>>>;
|
||||
pub fn routes() -> Router {
|
||||
let router = Router::new()
|
||||
.route("/", get(hello).post(hello))
|
||||
.route("/json", get(json::json).post(json::json))
|
||||
.layer(
|
||||
ServiceBuilder::new()
|
||||
.layer(middleware::from_fn(add_version))
|
||||
.layer(CorsLayer::permissive())
|
||||
.layer(TimeoutLayer::new(Duration::from_secs(15)))
|
||||
.layer(CompressionLayer::new()),
|
||||
.layer(TimeoutLayer::new(Duration::from_secs(15))),
|
||||
)
|
||||
.fallback(fallback);
|
||||
logging_route(router)
|
||||
|
Reference in New Issue
Block a user