provides the infrastructure for the webapi including setting up the server and the routes. Implements get_users as a test route
35 lines
859 B
Rust
35 lines
859 B
Rust
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()
|
|
}
|
|
}
|