build: scaffold axum api server entry
This commit is contained in:
9
server-rs/apps/api-server/Cargo.toml
Normal file
9
server-rs/apps/api-server/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "api-server"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
axum = "0.8"
|
||||
tokio = { version = "1", features = ["macros", "rt-multi-thread", "net"] }
|
||||
@@ -14,17 +14,22 @@
|
||||
|
||||
## 2. 当前阶段说明
|
||||
|
||||
当前提交仅完成目录占位,不提前进入具体实现。
|
||||
当前目录已经完成以下基础骨架:
|
||||
|
||||
1. 目录占位
|
||||
2. `Cargo.toml`
|
||||
3. `src/main.rs`
|
||||
4. `src/app.rs`
|
||||
5. `src/state.rs`
|
||||
|
||||
后续与本 package 直接相关的任务包括:
|
||||
|
||||
1. 搭建 `main.rs` / `Router` / `with_state`
|
||||
2. 接入统一配置加载
|
||||
3. 接入统一日志与 tracing
|
||||
4. 接入 `request_id`
|
||||
5. 接入统一错误处理中间件
|
||||
6. 接入 response envelope
|
||||
7. 接入 `/healthz`
|
||||
1. 接入统一配置加载
|
||||
2. 接入统一日志与 tracing
|
||||
3. 接入 `request_id`
|
||||
4. 接入统一错误处理中间件
|
||||
5. 接入 response envelope
|
||||
6. 接入 `/healthz`
|
||||
|
||||
## 3. 边界约束
|
||||
|
||||
|
||||
8
server-rs/apps/api-server/src/app.rs
Normal file
8
server-rs/apps/api-server/src/app.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use axum::Router;
|
||||
|
||||
use crate::state::AppState;
|
||||
|
||||
// 统一由这里构造 Axum 路由树,后续再逐项挂接中间件与业务路由。
|
||||
pub fn build_router(state: AppState) -> Router {
|
||||
Router::new().with_state(state)
|
||||
}
|
||||
20
server-rs/apps/api-server/src/main.rs
Normal file
20
server-rs/apps/api-server/src/main.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
mod app;
|
||||
mod state;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
use crate::{app::build_router, state::AppState};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), std::io::Error> {
|
||||
// 当前阶段先用固定地址启动最小骨架,后续再切到统一配置加载。
|
||||
let bind_address = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
let listener = TcpListener::bind(bind_address).await?;
|
||||
|
||||
let state = AppState::new();
|
||||
let router = build_router(state);
|
||||
|
||||
axum::serve(listener, router).await
|
||||
}
|
||||
9
server-rs/apps/api-server/src/state.rs
Normal file
9
server-rs/apps/api-server/src/state.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
// 当前阶段先保留最小共享状态壳,后续逐步接入配置、客户端与平台适配。
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct AppState;
|
||||
|
||||
impl AppState {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user