从零到第一个可运行的 Web 应用。Zenith 默认仅启用最小核心,本教程带你逐步开启所需能力。
在 Cargo.toml 中按需引入。Zenith 是 feature-gated facade,默认只启用 api + core。
# 最小类型层(仅 CanonicalRequest/Response)
[dependencies]
zenith = { path = "../zenith", default-features = false, features = ["api"] }
# 仅 Web 框架(不拖入 AF_XDP / eBPF / 数据面)
zenith = { path = "../zenith", default-features = false, features = ["web"] }
# 完整生产协议栈
zenith = { path = "../zenith", features = ["full-stack"] }
# 全部能力(含测试工具)
zenith = { path = "../zenith", features = ["full"] }use zenith::api::{CanonicalRequest, Method};
fn main() {
let mut req = CanonicalRequest::empty();
req.method = Method::Get;
req.set_path("/api/users");
assert!(req.path() == "/api/users");
}启用 web 特性后即可使用 App。编译期 Trie 路由实现运行期零开销分发。
use zenith::App;
use zenith::api::{CanonicalRequest, CanonicalResponse};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut app = App::new();
// GET /health → 204
app.get("/health", |_req, _params| {
Ok(CanonicalResponse::new(204))
});
// GET /users/:id → 带路径参数
app.get("/users/:id", |req, params| {
let id = params.get("id").unwrap_or("unknown");
let mut resp = CanonicalResponse::new(200);
resp.set_body(format!("user: {{}}", id));
Ok(resp)
});
app.run("0.0.0.0:8080")
}Zenith 仓库自带多个示例,覆盖不同能力组合。运行前需确保已启用对应 feature(见 Cargo.toml 的 [[example]] 段的 required-features)。
# 最小 Web 服务器
cargo run --example hello_server
# 完整生产协议栈(H1/H2/H3 + TLS + WAF + 代理)
cargo run --example full_feature_server
# L4 转发 / 中继
cargo run --example l4_test
cargo run --example l4_relay_test
# 指纹识别与阻断
cargo run --example fingerprint_block_test
cargo run --example fingerprint_ext_test
cargo run --example fingerprint_live_server
# 自动降级
cargo run --example auto_degrade_test启动 hello_server 后,用 curl 验证三种协议与 TLS:
# HTTP/1.1
curl -i http://localhost:8080/health
# HTTP/2(需启用 TLS)
curl --http2 -k https://localhost:8443/
# HTTP/3(QUIC)
curl --http3 -k https://localhost:8443/