Expand description
Β§Sword - Rust Web Framework
Sword is a modern, fast, and ergonomic web framework for Rust, built on top of Axum. It provides a clean API, powerful middleware system, and built-in features for rapid web development.
Β§π Quick Start
use sword::prelude::*;
#[controller("/api")]
struct ApiController;
#[routes]
impl ApiController {
#[get("/hello")]
async fn hello() -> HttpResponse {
HttpResponse::Ok().message("Hello, World!")
}
}
#[sword::main]
async fn main() {
let app = Application::builder()?
.with_controller::<ApiController>()
.build();
app.run().await?;
}
Β§π― Core Features
- π£οΈ Macro-based routing - Clean and intuitive route definitions using
#[get]
,#[post]
, etc. - π JSON-first design - Built-in JSON serialization/deserialization support
- β
Request validation - Automatic validation using
serde
andvalidator
crates - π RFC-compliant HTTP responses - Standards-compliant HTTP handling
- πΈοΈ Express-like Context - Rich request context with utility methods
- π Dependency Injection - Optional DI support using
shaku
crate - π§© Middleware system - Flexible middleware at route and controller levels
- π Async by default - Built on
tokio
andaxum
for high performance
Β§π¦ Optional Features
Enable additional functionality by adding features to your Cargo.toml
:
[dependencies]
sword = { version = "0.1.7", features = ["cookies", "multipart", "helmet"] }
Available features:
multipart
- File upload supportcookies
- Cookie handlinghelmet
- Security headers middlewareshaku-di
- Dependency injection
Β§π Examples
Check out the comprehensive examples in the repository:
- Basic server - Simple HTTP server setup
- Middleware - Custom middleware implementation
- Data validation - Request validation examples
- File uploads - Multipart form handling
- Dependency injection - DI patterns
- State management - Shared application state
ModulesΒ§
- core
- Core framework components for application setup and configuration.
- errors
- Error types and error handling utilities.
- prelude
- The prelude module contains the most commonly used items from the Sword framework.
- web
- Web-related components for handling HTTP requests and responses.
MacrosΒ§
- next
- A macro to simplify the next middleware call in the middleware chain.
It takes the current context and the next middleware in the chain,
and returns a
Result
with the response of the next middleware. This macro is used to avoid boilerplate code in middleware implementations. It is used in thehandle
method of theMiddleware
trait.
Attribute MacrosΒ§
- main
- This is just a re-export of
tokio::main
to simplify the initial setup of