diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/response.rs | 32 | ||||
| -rw-r--r-- | src/routers/account.rs | 21 |
3 files changed, 48 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs index ff04b57..c53664a 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | mod error; | ||
| 2 | mod response; | ||
| 1 | mod routers; | 3 | mod routers; |
| 2 | 4 | ||
| 3 | use tokio::net::TcpListener; | 5 | use tokio::net::TcpListener; |
diff --git a/src/response.rs b/src/response.rs new file mode 100644 index 0000000..8d505a5 --- /dev/null +++ b/src/response.rs | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | use axum::response::{IntoResponse, Response}; | ||
| 2 | use serde::Serialize; | ||
| 3 | use serde_json::json; | ||
| 4 | |||
| 5 | pub enum ApiResponse<T> { | ||
| 6 | Success(T), | ||
| 7 | Fail(T), | ||
| 8 | Error(String), | ||
| 9 | } | ||
| 10 | |||
| 11 | impl<T> IntoResponse for ApiResponse<T> | ||
| 12 | where | ||
| 13 | T: Serialize, | ||
| 14 | { | ||
| 15 | fn into_response(self) -> Response { | ||
| 16 | axum::Json(match self { | ||
| 17 | ApiResponse::Success(data) => json!({ | ||
| 18 | "status": "success", | ||
| 19 | "data": data | ||
| 20 | }), | ||
| 21 | ApiResponse::Fail(data) => json!({ | ||
| 22 | "status": "fail", | ||
| 23 | "data": data | ||
| 24 | }), | ||
| 25 | ApiResponse::Error(message) => json!({ | ||
| 26 | "status": "error", | ||
| 27 | "message": message | ||
| 28 | }), | ||
| 29 | }) | ||
| 30 | .into_response() | ||
| 31 | } | ||
| 32 | } | ||
diff --git a/src/routers/account.rs b/src/routers/account.rs index e8c9753..8192133 100644 --- a/src/routers/account.rs +++ b/src/routers/account.rs | |||
| @@ -2,16 +2,23 @@ use axum::Router; | |||
| 2 | use axum::response::IntoResponse; | 2 | use axum::response::IntoResponse; |
| 3 | use axum::routing::{get, post}; | 3 | use axum::routing::{get, post}; |
| 4 | 4 | ||
| 5 | pub(crate) fn router() -> Router { | 5 | use crate::response::ApiResponse; |
| 6 | Router::new() | ||
| 7 | .route("/me", get(me)) | ||
| 8 | .route("/login", post(login)) | ||
| 9 | } | ||
| 10 | 6 | ||
| 11 | async fn me() -> impl IntoResponse { | 7 | async fn me() -> impl IntoResponse { |
| 12 | "Me" | 8 | ApiResponse::Success("Me") |
| 9 | } | ||
| 10 | |||
| 11 | async fn register() -> impl IntoResponse { | ||
| 12 | ApiResponse::Success("Register") | ||
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | async fn login() -> impl IntoResponse { | 15 | async fn login() -> impl IntoResponse { |
| 16 | "Login" | 16 | ApiResponse::Success("Login") |
| 17 | } | ||
| 18 | |||
| 19 | pub(crate) fn router() -> Router { | ||
| 20 | Router::new() | ||
| 21 | .route("/me", get(me)) | ||
| 22 | .route("/register", post(register)) | ||
| 23 | .route("/login", post(login)) | ||
| 17 | } | 24 | } |
