axum-base

v0.0.1

Basic Axum starter file archiecture recipe

_ _signor_p_ ยท 0 downloads ยท Updated 7/13/2026
rustaxumnexrecipebackendapi
xnex install axum-base GitHub repo

๐Ÿ“ฆ Axum Base Recipe

A pluggable, highly modular starter architecture for building decoupled and scalable APIs with Rust and Axum.

This recipe establishes a clear feature-driven isolation pattern, allowing developers to drop in or remove standalone modules effortlessly without breaking the application core.


๐Ÿ“‚ Architecture Overview

Every feature (e.g., health, auth, users) lives inside its own isolated folder under src/modules/. Modules expose only their unified router to the central router system.

text
src/
โ”œโ”€โ”€ main.rs          # Application entry point (Server boot & listener)
โ”œโ”€โ”€ lib.rs           # Crate root exporting core routing and modules
โ”œโ”€โ”€ routes.rs        # Central router hub (Where modules are mounted via .nest())
โ””โ”€โ”€ modules/
    โ”œโ”€โ”€ mod.rs       # Module registry (Enables/disables standalone modules)
    โ””โ”€โ”€ health/      # Example of an isolated feature module
        โ”œโ”€โ”€ mod.rs   # Entry point exposing the feature's router
        โ””โ”€โ”€ routes.rs # Feature-specific handlers and endpoints

๐Ÿš€ Key Design Philosophies

  • Strict Isolation: Modules do not cross-contaminate. Code inside modules/health/ knows nothing about external modules.
  • Plug & Play (Router::nest): To attach or detach a feature, you only add or remove a single line in src/routes.rs.
  • Crate-Ready: The architecture separates binary logic (main.rs) from library logic (lib.rs), making it trivial to extract features into standalone open-source crates later.

๐Ÿ› ๏ธ Quick Start

1. Installation & Recipe Setup

The setup lifecycle automatically handles configuration and pulls the essential asynchronous and serialization primitives:

bash
cargo add axum
cargo add tokio --features full
cargo add serde --features derive

2. Booting the Server

Run the application locally:

bash
cargo run

Once booted, the application mounts your decoupled modules under the specified path prefixes:

  • Health Check Endpoint: GET [http://127.0.0.1:3000/api/health](http://127.0.0.1:3000/api/health)

๐Ÿ”Œ How to Add a New Feature Module

To expand your API with a new module (e.g., a users module) without causing architectural drift, follow these three steps:

  1. Create the Folder Structure: Create src/modules/users/mod.rs and write your handlers/routes inside it, exposing a single pub fn router() -> Router function.
  2. Register the Module: Open src/modules/mod.rs and register your new folder:
rust
pub mod health;
pub mod users; // <-- Add this
  1. Mount the Route Hub: Inject the standalone router into the gateway tree in src/routes.rs:
rust
pub fn create_router() -> Router {
    Router::new()
        .nest("/api/health", modules::health::router())
        .nest("/api/users", modules::users::router()) // <-- Mount here
}