mirror of
https://github.com/DefectingCat/phthonus
synced 2025-07-15 16:41:32 +00:00
feat(axum-xitca): add json route
This commit is contained in:
20
frameworks/Rust/axum-xitca/src/routes/json.rs
Normal file
20
frameworks/Rust/axum-xitca/src/routes/json.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use axum::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{RouteResponse, RouteResult};
|
||||
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
pub struct JsonData {
|
||||
pub name: Cow<'static, str>,
|
||||
}
|
||||
|
||||
pub async fn json() -> RouteResult<JsonData> {
|
||||
let data = JsonData { name: "xfy".into() };
|
||||
let res = RouteResponse {
|
||||
data,
|
||||
..Default::default()
|
||||
};
|
||||
Ok(Json(res))
|
||||
}
|
@ -19,6 +19,8 @@ use crate::{
|
||||
middlewares::{add_version, logging_route},
|
||||
};
|
||||
|
||||
mod json;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct RouteResponse<T>
|
||||
where
|
||||
@ -29,11 +31,24 @@ where
|
||||
message: Option<Cow<'static, str>>,
|
||||
data: T,
|
||||
}
|
||||
impl<T> Default for RouteResponse<T>
|
||||
where
|
||||
T: Serialize + Default,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
code: ErrorCode::Normal,
|
||||
message: None,
|
||||
data: T::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
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))
|
||||
|
Reference in New Issue
Block a user