aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs2
-rw-r--r--src/response.rs32
-rw-r--r--src/routers/account.rs21
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 @@
1mod error;
2mod response;
1mod routers; 3mod routers;
2 4
3use tokio::net::TcpListener; 5use 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 @@
1use axum::response::{IntoResponse, Response};
2use serde::Serialize;
3use serde_json::json;
4
5pub enum ApiResponse<T> {
6 Success(T),
7 Fail(T),
8 Error(String),
9}
10
11impl<T> IntoResponse for ApiResponse<T>
12where
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;
2use axum::response::IntoResponse; 2use axum::response::IntoResponse;
3use axum::routing::{get, post}; 3use axum::routing::{get, post};
4 4
5pub(crate) fn router() -> Router { 5use crate::response::ApiResponse;
6 Router::new()
7 .route("/me", get(me))
8 .route("/login", post(login))
9}
10 6
11async fn me() -> impl IntoResponse { 7async fn me() -> impl IntoResponse {
12 "Me" 8 ApiResponse::Success("Me")
9}
10
11async fn register() -> impl IntoResponse {
12 ApiResponse::Success("Register")
13} 13}
14 14
15async fn login() -> impl IntoResponse { 15async fn login() -> impl IntoResponse {
16 "Login" 16 ApiResponse::Success("Login")
17}
18
19pub(crate) fn router() -> Router {
20 Router::new()
21 .route("/me", get(me))
22 .route("/register", post(register))
23 .route("/login", post(login))
17} 24}