Skip to main content

sz_rust_examples/
lib.rs

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