Skip to main content

sz_rust_examples/
lib.rs

1//! SZ-Rust Examples — 示例库
2//!
3//! 提供 Hello World 端点的 router 构建函数,便于集成测试。
4
5#![forbid(unsafe_code)]
6#![warn(missing_docs)]
7
8use axum::{routing::get, Json, Router};
9use serde_json::{json, Value};
10use sz_rust_core::error::ErrorCode;
11use sz_rust_core::sql_string;
12use tower_http::trace::TraceLayer;
13
14/// Hello World 处理器
15///
16/// 返回标准 JSON 响应,对齐 PHP `renderJson($code=1, $msg='', $data=[])`:
17/// ```json
18/// { "code": 1, "msg": "hello", "data": {} }
19/// ```
20pub async fn hello() -> Json<Value> {
21    // 验证编译时 SQL 校验宏可用
22    let _sql = sql_string!("SELECT 1 FROM dual");
23
24    // 标准响应:对齐 PHP renderJson(code=1, msg='hello', data=[])
25    Json(json!({
26        "code": ErrorCode::Success as i32,
27        "msg": "hello",
28        "data": {},
29    }))
30}
31
32/// 健康检查处理器
33pub async fn health() -> Json<Value> {
34    Json(json!({
35        "code": ErrorCode::Success as i32,
36        "msg": "ok",
37        "data": {
38            "status": "healthy",
39            "version": env!("CARGO_PKG_VERSION"),
40        },
41    }))
42}
43
44/// 构建示例 Router
45pub fn build_router() -> Router {
46    Router::new()
47        .route("/", get(hello))
48        .route("/health", get(health))
49        .layer(TraceLayer::new_for_http())
50}