feat(project): init
This commit is contained in:
48
src/handlers/mod.rs
Normal file
48
src/handlers/mod.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use crate::middleware::TenantId;
|
||||
use crate::models::{CreateUserRequest, UserResponse};
|
||||
use crate::services::AuthService;
|
||||
use axum::{Json, extract::State};
|
||||
use common_telemetry::AppError; // 引入刚刚写的中间件类型
|
||||
|
||||
// 状态对象,包含 Service
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
pub auth_service: AuthService,
|
||||
}
|
||||
|
||||
/// 注册接口
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/auth/register",
|
||||
request_body = CreateUserRequest,
|
||||
responses(
|
||||
(status = 201, description = "User created", body = UserResponse),
|
||||
(status = 400, description = "Bad request")
|
||||
),
|
||||
params(
|
||||
("X-Tenant-ID" = String, Header, description = "Tenant UUID")
|
||||
)
|
||||
)]
|
||||
pub async fn register_handler(
|
||||
// 1. 自动注入 TenantId (由中间件解析)
|
||||
TenantId(tenant_id): TenantId,
|
||||
// 2. 获取全局状态中的 Service
|
||||
State(state): State<AppState>,
|
||||
// 3. 获取 Body
|
||||
Json(payload): Json<CreateUserRequest>,
|
||||
) -> Result<Json<UserResponse>, AppError> {
|
||||
let user = state
|
||||
.auth_service
|
||||
.register(tenant_id, payload)
|
||||
.await
|
||||
.map_err(AppError::BadRequest)?;
|
||||
|
||||
// 转换为 Response DTO (隐藏密码等敏感信息)
|
||||
let response = UserResponse {
|
||||
id: user.id,
|
||||
email: user.email.clone(),
|
||||
// ...
|
||||
};
|
||||
|
||||
Ok(Json(response))
|
||||
}
|
||||
Reference in New Issue
Block a user