# Ruxno

A fast, ergonomic Rust web framework with clean architecture principles.

## Architecture

Ruxno follows a layered clean architecture:

```
┌─────────────────────────────────────────┐
│         Application (App Facade)        │  ← Public API
├─────────────────────────────────────────┤
│      Pipeline (Dispatcher/Executor)     │  ← Orchestration
├─────────────────────────────────────────┤
│    Domain (Request/Response/Context)    │  ← Business Models
├─────────────────────────────────────────┤
│      Core (Handler/Middleware Traits)   │  ← Abstractions
├─────────────────────────────────────────┤
│   Infrastructure (Server/HTTP/Hyper)    │  ← External Dependencies
└─────────────────────────────────────────┘
```

## Features

- **Clean Architecture**: Layered design with clear separation of concerns
- **Performance**: Pre-computed middleware chains, zero-copy requests
- **Unified Middleware System**: Explicit phases (PreRouting/PostRouting) with pattern matching
- **Type Safety**: Generic environment parameters, type-safe extensions
- **Testability**: Each layer can be tested independently
- **Ergonomic API**: Hono.js-inspired API for familiarity

## Quick Start

```rust
use ruxno::prelude::*;

#[tokio::main]
async fn main() {
    let app = App::new();

    app.get("/", async |c: Context| {
        c.text("Hello, World!")
    });

    app.listen("127.0.0.1:3000").await.unwrap();
}
```

## Middleware System

Ruxno features a unified middleware system with explicit execution phases:

### Pre-Routing Middleware

Runs **before** route matching. Use for:

- CORS preflight requests
- Health checks that should bypass routing
- Early request rejection (rate limiting, IP blocking)

**Important**: Cannot access route parameters.

```rust
use ruxno::prelude::*;

let app = App::new();

// Global pre-routing middleware
app.use_before_routing(|ctx: Context, next: Next| async move {
    println!("Before routing: {}", ctx.req.path());
    next.run(ctx).await
});

// Path-specific pre-routing middleware
app.use_before_routing_on("/health", |ctx: Context, _next: Next| async move {
    Ok(Response::json(&serde_json::json!({"status": "ok"})))
});
```

### Post-Routing Middleware (Default)

Runs **after** route matching. Use for:

- Authentication and authorization
- Request validation
- Logging with route context
- Response transformation

**Benefit**: Has access to route parameters.

```rust
use ruxno::prelude::*;

let app = App::new();

// Global post-routing middleware
app.r#use(|ctx: Context, next: Next| async move {
    println!("After routing: {}", ctx.req.path());
    next.run(ctx).await
});

// Path-specific post-routing middleware
app.use_on("/api/*", |ctx: Context, next: Next| async move {
    // Auth middleware for /api routes
    next.run(ctx).await
});

// Method + path-specific middleware
app.on(Method::POST, "/api/*", async |ctx: Context, next: Next| {
    // CSRF validation for POST requests to /api
    next.run(ctx).await
});
```

## Examples

See the `examples/` directory for complete examples:

- `01_hello_world` - MVC architecture with middleware phases
- `02_websocket` - WebSocket support

## License

MIT OR Apache-2.0
