Skip to content

A wrapper around Axum for a standard REST API built for Rust

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

jrkosinski/rust-api

RustAPI

FastAPI-inspired REST framework for Rust

Crates.io Documentation License Build Status Rust Version

Motivation: to make it as easy as possible to spin up a quick REST API in Rust with minimal plumbing code.

FastAPI in Python, and NestJS in JS/TS, make it easy to spin up a REST API. There are plenty of good reasons in which you might need a REST API defined in Rust, providing access (perhaps internal) to code that is best done in Rust. What I want is a FastAPI-like experience in Rust. This crate attempts to give that, as much as possible. The class-first definition of FastAPI, the dependency-injection features of NestJS. It offers:

  • Route Macros - FastAPI-style endpoint definitions
  • Dependency Injection - Type-safe service container
  • Performance - Built on Axum + Tokio
  • Type Safety - Leverage Rust's type system
  • Future: Auto OpenAPI - Documentation that stays in sync (coming soon)

Status: Active Development | Not yet production-ready

Quick Start

use rust-api::prelude::*;

#[derive(Serialize, Deserialize)]
struct User {
    id: String,
    name: String,
}

#[get("/")]
async fn hello() -> &'static str {
    "Hello, rust-api!"
}

#[get("/users/{id}")]
async fn get_user(Path(id): Path<String>) -> Json<User> {
    Json(User {
        id: id.clone(),
        name: format!("User {}", id)
    })
}

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route(__hello_route, routing::get(hello))
        .route(__get_user_route, routing::get(get_user));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
        .await
        .unwrap();

    axum::serve(listener, app).await.unwrap();
}

Features

✅ Implemented

  • Route Macros: #[get], #[post], #[put], #[delete], #[patch]
  • DI Container: Type-safe service registration and resolution
  • Prelude Module: One import for everything you need
  • Examples: Working hello_world and full-featured examples

Coming Soon

  • Inject<T> Extractor: Automatic dependency injection in handlers
  • Validation: #[derive(Validate)] with automatic error responses
  • OpenAPI Generation: Auto-generated Swagger docs
  • Request-Scoped Services: Per-request service instances
  • Testing Utilities: Easy integration testing

Examples

Run the examples to see the framework working:

# Minimal hello world
cargo run --example hello_world

# Full-featured example
cargo run --example with_macros

# Demo app with DI
cargo run

Then test the endpoints:

curl http://localhost:3000/
curl http://localhost:3000/users/42

Architecture

rust-api/
├── crates/
│   ├── rust-api/           # Main crate (DI, app builder, server, router)
│   └── rust-api-macros/    # Route macros (#[get], etc.)
├── examples/
│   ├── hello_world.rs     # Minimal example
│   ├── with_macros.rs     # Full-featured example
│   └── basic-api/         # Complete app with controllers and services
└── Cargo.toml             # Workspace configuration

Comparison

Feature rust-api axum actix-web poem rocket
Route Macros
Built-in DI
Auto OpenAPI In Progress
FastAPI-like DX ~ ~
Performance High High High High High

Documentation

Roadmap

Phase 1: Core

  • DI Container
  • Route Macros
  • Examples

Phase 2: DX Improvements (In Progress)

  • Inject<T> extractor
  • Better route registration
  • Macro-generated app builder
  • Reflection-like definition without actual reflection: define a class that becomes the API

Phase 3: Validation (Planned)

  • #[derive(Validate)]
  • Automatic validation
  • Structured error responses

Phase 4: OpenAPI (Planned)

  • Schema generation
  • Swagger UI
  • ReDoc support

Why RustAPI?

Python/FastAPI developers get Rust performance with familiar patterns.

TypeScript/NestJS developers get dependency injection in Rust.

Rust developers get FastAPI-level developer experience.

Contributing

This is currently in active development. Contributions welcome!

License

This project is licensed under either of:

at your option.

Inspiration

  • FastAPI (Python) - Amazing DX, automatic docs
  • NestJS (TypeScript) - Dependency injection, modules
  • Axum (Rust) - Performance, type safety

Built using Rust, Axum, and Tokio.

About

A wrapper around Axum for a standard REST API built for Rust

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages