Astrea
A file-system based routing framework for Axum, inspired by Nitro and H3.
crates.io | GitHub | Documentation
Features
- Simple, unified function signature - All handlers follow the same pattern:
async fn handler(event: Event) -> Result<Response> - Declarative parameter extraction - Access request data through helper functions instead of complex extractors
- File-based routing - Routes are automatically generated from your filesystem structure
- Type-safe - Full Rust type safety with compile-time route generation
- Axum ecosystem compatible - Works seamlessly with Axum middleware and ecosystem
- Zero runtime overhead - Route generation happens at compile time via procedural macros
Installation
Add this to your Cargo.toml:
[]
= "0.0"
= { = "1", = ["full"] }
Or use cargo-edit:
Quick Start
1. Create your route files
Create a routes/ directory in your project root and add route files:
routes/
├── index.get.rs # GET /
├── users/
│ ├── index.get.rs # GET /users
│ └── [id].get.rs # GET /users/:id
└── posts/
└── index.post.rs # POST /posts
2. Define your handlers
// routes/index.get.rs
use *;
pub async
// routes/users/[id].get.rs
use *;
pub async
3. Generate routes and run
// src/main.rs
async
Route File Naming Convention
Routes are generated based on the file structure and naming:
| File Pattern | Route | Method |
|---|---|---|
index.get.rs |
/ |
GET |
index.post.rs |
/ |
POST |
users.get.rs |
/users |
GET |
users/[id].get.rs |
/users/:id |
GET |
posts/[...slug].get.rs |
/posts/*slug |
GET |
Dynamic parameters use square brackets:
[id]→:id(single path parameter)[...slug]→*slug(catch-all parameter)
Request Data Extraction
Astrea provides helper functions to access request data:
use *;
pub async
Response Helpers
use *;
// JSON response
json?
// Text response
text
// HTML response
html
// Redirect
redirect?
// Custom status and headers
json?
.status
.header
Examples
Run the examples with cargo run --example <name>:
# Simple hello world
# JSON API with route parameters
# Request data extraction (query params, headers, JSON body)
For a complete application example, see the basic-app/ directory.
Project Structure
astrea/
├── astrea-macro/ # Procedural macros
│ └── src/lib.rs # #[route] and generate_routes! macros
├── basic-app/ # Complete example application (independent project)
│ └── src/routes/ # File-based route examples
├── examples/ # Simple code examples
│ ├── hello.rs # Minimal hello world
│ ├── json_api.rs # JSON API with parameters
│ └── request_data.rs # Request data extraction
├── benches/ # Performance benchmarks
├── src/
│ ├── lib.rs # Main library exports
│ ├── event.rs # Event type
│ ├── extract.rs # Helper extraction functions
│ ├── response.rs # Response builders
│ ├── error.rs # Error types
│ └── router.rs # Router utilities
└── tests/ # Integration tests
Author
TNXG (Asahi Shiori)
- GitHub: @TNXG
- Blog: https://tnxg.moe
- Email: tnxg@outlook.jp
License
This project is licensed under the MIT License - see the LICENSE file for details.