Expand description
§Astrea - File-based Router for Axum
/ # Astrea - 基于 Axum 的文件路由器
Astrea is a file-system based router for Axum, inspired by Nitro and H3.
Astrea 是一个受 Nitro 和 H3 启发的 Axum 文件路由器。
§Features
§特性
-
Simple unified handler signature - All handlers follow the same pattern:
async fn handler(event: Event) -> Result<Response>简单统一的处理函数签名 - 所有处理函数遵循相同模式:async fn handler(event: Event) -> Result<Response> -
Declarative parameter extraction - Access request data through helper functions instead of complex Axum extractor signatures 声明式参数提取 - 通过辅助函数访问请求数据,无需复杂的 Axum 提取器签名
-
File-based routing - Routes are automatically generated from your filesystem structure at compile time 基于文件的路由 - 在编译时根据文件系统结构自动生成路由
-
Type-safe - Full Rust type safety with compile-time route generation 类型安全 - 完整的 Rust 类型安全,编译时生成路由
-
Axum ecosystem compatible - Works seamlessly with Axum middleware 兼容 Axum 生态 - 与 Axum 中间件无缝协作
§Quick Start
§快速开始
use astrea::prelude::*;
use serde_json::json;
// routes/index.get.rs
#[route]
async fn handler(event: Event) -> Result<Response> {
let name = get_param(&event, "name").unwrap_or("World");
json(json!({ "message": format!("Hello, {}!", name) }))
}§Route File Convention
§路由文件规则
Routes are generated from the routes/ directory:
路由从 routes/ 目录生成:
routes/index.get.rs→GET /routes/users.get.rs→GET /usersroutes/users/[id].get.rs→GET /users/:idroutes/posts/[...slug].get.rs→GET /posts/*slug
§Module Organization
§模块组织
Re-exports§
pub use error::RouteError;pub use event::Event;pub use response::Response;pub use axum;pub use bytes;pub use comfy_table;pub use serde;pub use serde_json;pub use tokio;pub use tower;pub use tower_http;pub use tracing;pub use tokio_stream;
Modules§
- error
- Error types for Astrea
- event
- Event type and related functionality
- extract
- Helper functions for extracting data from Event
- middleware
- Middleware system for Astrea
- prelude
- Prelude module with common imports
- response
- Response type and builders
- router
- 文件路由器
- serve
- Serve services.
- sse
- Server-Sent Events (SSE) support
- ws
- WebSocket support
Macros§
- generate_
routes - Procedural macro to generate routes from filesystem
Functions§
- serve
- Serve the service with the supplied listener.