spikard-http 0.13.0

High-performance HTTP server for Spikard with tower-http middleware stack
Documentation

spikard-http

High-performance HTTP server for Spikard with a complete tower-http middleware stack, JSON Schema validation, and cross-language handler execution.

Features

  • Axum-based routing with zero-allocation path matching
  • Tower middleware stack: compression, rate limiting, timeouts, CORS, request IDs, auth
  • JSON Schema validation for request/response
  • Cross-language handlers via the Handler trait (Python, Node.js, Ruby, PHP, Elixir)
  • OpenAPI 3.1 and AsyncAPI spec generation
  • WebSocket and SSE support
  • Graceful shutdown with in-flight request completion
  • Static file serving with caching

Installation

[dependencies]
spikard-http = "0.13.0"

Quick Start

use spikard_http::{ServerConfig, start_server};
use spikard_core::{RouteConfig, Request, Response};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = ServerConfig {
        host: "0.0.0.0".to_string(),
        port: 8080,
        ..Default::default()
    };

    // Create routes with schemas
    let routes = vec![
        RouteConfig::get("/health")
            .handler("health", |_req| async {
                Ok(Response::new(200).with_body(r#"{"status": "ok"}"#))
            }),
    ];

    start_server(config, routes).await?;
    Ok(())
}

Middleware Stack

The default middleware stack (in order):

  1. Compression - gzip/brotli compression (configurable)
  2. Request ID - Unique request tracking
  3. Timeout - Request timeout enforcement
  4. Rate Limit - Per-IP rate limiting (if configured)
  5. Authentication - JWT/Bearer token validation (if configured)
  6. User-Agent - User agent parsing and validation
  7. CORS - Cross-origin resource sharing (if configured)
  8. Handler - Your application logic

See ServerConfig documentation for detailed configuration options.

Validation

Validate requests against JSON schemas:

use spikard_http::validation::ValidateRequest;
use serde_json::json;

let schema = json!({
    "type": "object",
    "properties": {
        "name": { "type": "string" },
        "age": { "type": "integer", "minimum": 0 }
    },
    "required": ["name"]
});

request.validate_body(&schema)?;

Related Crates

Documentation

License

MIT - See LICENSE for details