Crate sword

Crate sword 

Source
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 and validator 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 and axum 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 support
  • cookies - Cookie handling
  • helmet - Security headers middleware
  • shaku-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 the handle method of the Middleware trait.

Attribute MacrosΒ§

main
This is just a re-export of tokio::main to simplify the initial setup of