buster/api/src/routes/rest/mod.rs

43 lines
964 B
Rust
Raw Normal View History

2025-01-04 05:32:54 +08:00
use axum::{
body::Body,
http::{Response, StatusCode},
middleware as axum_middleware,
2025-01-04 05:32:54 +08:00
response::IntoResponse,
Json, Router,
};
use middleware::auth;
2025-01-04 05:32:54 +08:00
mod routes;
mod webhooks;
pub fn router() -> Router {
Router::new().nest("/", routes::router()).merge(
Router::new()
.nest("/webhooks", webhooks::router())
.route_layer(axum_middleware::from_fn(auth)),
2025-01-04 05:32:54 +08:00
)
}
pub enum ApiResponse<T> {
OK,
2025-03-22 02:54:54 +08:00
#[allow(dead_code)]
Created,
2025-01-04 05:32:54 +08:00
NoContent,
JsonData(T),
}
impl<T> IntoResponse for ApiResponse<T>
where
T: serde::Serialize,
{
fn into_response(self) -> Response<Body> {
match self {
Self::OK => (StatusCode::OK).into_response(),
Self::Created => (StatusCode::CREATED).into_response(),
Self::JsonData(data) => (StatusCode::OK, Json(data)).into_response(),
Self::NoContent => (StatusCode::NO_CONTENT).into_response(),
}
}
}