统一接口返回格式为 success/message/data 结构
This commit is contained in:
@@ -2,7 +2,7 @@ use axum::{
|
||||
extract::State,
|
||||
http::StatusCode,
|
||||
response::Json,
|
||||
routing::post,
|
||||
routing::{get, post},
|
||||
Router,
|
||||
};
|
||||
use bcrypt::hash;
|
||||
@@ -28,60 +28,65 @@ struct RegisterRequest {
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct RegisterResponse {
|
||||
struct ApiResponse<T> {
|
||||
success: bool,
|
||||
user_id: Option<Uuid>,
|
||||
message: String,
|
||||
data: Option<T>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct RegisterData {
|
||||
user_id: Uuid,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
|
||||
info!("Starting user-register service...");
|
||||
|
||||
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
let pool = sqlx::postgres::PgPool::connect(&database_url)
|
||||
.await
|
||||
.expect("Failed to connect to database");
|
||||
|
||||
|
||||
info!("Database connected");
|
||||
|
||||
|
||||
let state = Arc::new(AppState { db: pool });
|
||||
|
||||
|
||||
let app = Router::new()
|
||||
.route("/register", post(register_handler))
|
||||
.route("/health", axum::routing::get(health_handler))
|
||||
.route("/health", get(health_handler))
|
||||
.with_state(state);
|
||||
|
||||
|
||||
let port = env::var("SERVICE_PORT").unwrap_or_else(|_| "8080".to_string());
|
||||
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
||||
info!("User-register service listening on port {}", port);
|
||||
|
||||
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn register_handler(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Json(payload): Json<RegisterRequest>,
|
||||
) -> (StatusCode, Json<RegisterResponse>) {
|
||||
) -> (StatusCode, Json<ApiResponse<RegisterData>>) {
|
||||
info!("Registration attempt for user: {}", payload.username);
|
||||
|
||||
|
||||
// 参数校验
|
||||
if let Err(e) = payload.validate() {
|
||||
return (
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json(RegisterResponse {
|
||||
Json(ApiResponse {
|
||||
success: false,
|
||||
user_id: None,
|
||||
message: format!("Validation error: {}", e),
|
||||
data: None,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// 检查账号是否已存在
|
||||
let existing: Option<(Uuid,)> = sqlx::query_as(
|
||||
"SELECT id FROM user_login_account WHERE account = $1 AND deleted = FALSE"
|
||||
@@ -94,10 +99,10 @@ async fn register_handler(
|
||||
if existing.is_some() {
|
||||
return (
|
||||
StatusCode::CONFLICT,
|
||||
Json(RegisterResponse {
|
||||
Json(ApiResponse {
|
||||
success: false,
|
||||
user_id: None,
|
||||
message: "Username already exists".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -109,10 +114,10 @@ async fn register_handler(
|
||||
warn!("Password hashing failed: {}", e);
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(RegisterResponse {
|
||||
Json(ApiResponse {
|
||||
success: false,
|
||||
user_id: None,
|
||||
message: "Internal error".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -125,10 +130,10 @@ async fn register_handler(
|
||||
warn!("Transaction start failed: {}", e);
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(RegisterResponse {
|
||||
Json(ApiResponse {
|
||||
success: false,
|
||||
user_id: None,
|
||||
message: "Internal error".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -147,10 +152,10 @@ async fn register_handler(
|
||||
let _ = tx.rollback().await;
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(RegisterResponse {
|
||||
Json(ApiResponse {
|
||||
success: false,
|
||||
user_id: None,
|
||||
message: "Registration failed".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -169,10 +174,10 @@ async fn register_handler(
|
||||
let _ = tx.rollback().await;
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(RegisterResponse {
|
||||
Json(ApiResponse {
|
||||
success: false,
|
||||
user_id: None,
|
||||
message: "Registration failed".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -191,10 +196,10 @@ async fn register_handler(
|
||||
let _ = tx.rollback().await;
|
||||
return (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(RegisterResponse {
|
||||
Json(ApiResponse {
|
||||
success: false,
|
||||
user_id: None,
|
||||
message: "Registration failed".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -204,10 +209,10 @@ async fn register_handler(
|
||||
info!("User {} registered with id {}", payload.username, user_id);
|
||||
(
|
||||
StatusCode::CREATED,
|
||||
Json(RegisterResponse {
|
||||
Json(ApiResponse {
|
||||
success: true,
|
||||
user_id: Some(user_id),
|
||||
message: "User registered successfully".to_string(),
|
||||
data: Some(RegisterData { user_id }),
|
||||
}),
|
||||
)
|
||||
}
|
||||
@@ -215,16 +220,23 @@ async fn register_handler(
|
||||
warn!("Registration failed: {}", e);
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json(RegisterResponse {
|
||||
Json(ApiResponse {
|
||||
success: false,
|
||||
user_id: None,
|
||||
message: "Registration failed".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn health_handler() -> &'static str {
|
||||
"OK"
|
||||
async fn health_handler() -> (StatusCode, Json<ApiResponse<()>>) {
|
||||
(
|
||||
StatusCode::OK,
|
||||
Json(ApiResponse {
|
||||
success: true,
|
||||
message: "OK".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user