webapi, inital schema and routes (get_users)
provides the infrastructure for the webapi including setting up the server and the routes. Implements get_users as a test route
This commit is contained in:
parent
1400b2fc95
commit
92763fd7dc
7 changed files with 197 additions and 0 deletions
35
koucha/src/bin/webapi/routes/mod.rs
Normal file
35
koucha/src/bin/webapi/routes/mod.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use serde::Serialize;
|
||||
use serde_json::json;
|
||||
use axum::{Json, Router, response::IntoResponse, routing::get};
|
||||
use reqwest::StatusCode;
|
||||
|
||||
mod get_users;
|
||||
|
||||
use crate::AppState;
|
||||
|
||||
pub type ApiResult<T> = Result<ApiResponse<T>, ApiError>;
|
||||
|
||||
pub struct ApiResponse<T>(pub StatusCode, pub T);
|
||||
|
||||
pub struct ApiError {
|
||||
pub status: StatusCode,
|
||||
pub error: &'static str,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/get_users", get(get_users::handler))
|
||||
}
|
||||
|
||||
impl IntoResponse for ApiError {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
let body = json!({ "error": self.error, "message": self.message });
|
||||
(self.status, Json(body)).into_response()
|
||||
}
|
||||
}
|
||||
impl<T: Serialize> IntoResponse for ApiResponse<T> {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
(self.0, Json(self.1)).into_response()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue