mirror of
https://github.com/kristoferssolo/axum-template.git
synced 2025-10-21 17:20:35 +00:00
| config | ||
| migrations | ||
| scripts | ||
| src | ||
| tests/api | ||
| .dockerfile | ||
| .env.example | ||
| .gitignore | ||
| Cargo.toml | ||
| Dockerfile | ||
| justfile | ||
| LICENSE | ||
| README.md | ||
Axum Askama Template
This is a Rust-based web application template built with Axum for the web framework and Askama for templating. It follows a structured architecture inspired by "Zero to Production in Rust" to promote maintainability, testability, and scalability.
Features
- Axum Web Framework: A fast and ergonomic web framework for Rust.
- Askama Templating: A compile-time HTML templating engine for Rust, ensuring type safety and performance.
- Structured Architecture: A layered architecture with clear separation of concerns, making the codebase easier to understand and maintain.
- Configuration Management: Uses
configcrate for managing application settings with support for base, local, and production configurations. - Database Integration: Uses
sqlxfor asynchronous database interactions with PostgreSQL. - Telemetry: Uses
tracingandtracing-bunyan-formatterfor structured logging and tracing. - Middleware: Includes request logging middleware using
tower-http. - Dependency Injection: Uses Axum's
with_statefor dependency injection, making it easy to provide shared state to route handlers. - Docker Support: Includes a
Dockerfilefor easy containerization. - Justfile: Includes a
Justfilefor common development tasks. cargo-generateTemplate: Easily generate new projects from this template usingcargo-generate.
Getting Started
Prerequisites
- Rust toolchain (stable or nightly)
- PostgreSQL database
- Docker (optional, for containerization)
- Just (optional, for task running)
cargo-generate(recommended for generating new projects)bunyan(optional, for formatting console logs)
Configuration
- Database:
- Create a PostgreSQL database.
- Update the database connection string in
config/base.toml(andlocal.tomlorproduction.tomlas needed).
- Environment Variables:
- Set any required environment variables (e.g., for email sending).
Creating a New Project
- Install
cargo-generate:
cargo install cargo-generate
- Generate a new project:
cargo generate kristoferssolo/axum-template
Running the Application
- Navigate to the new project directory:
cd <project_name>
- Initialize the database:
bash scripts/init_db
- Run the application:
cargo run
Or, if you have just installed:
just dev
- Access the application:
Open your browser and navigate to http://localhost:8000 (or the configured port).
Running Tests
cargo test
Directory Structure
├── Cargo.toml
├── config/ # Handles loading and merging configuration files.
│ ├── base.toml # Base configuration
│ ├── local.toml # Local development overrides
│ └── production.toml # Production configuration overrides
├── Dockerfile
├── justfile
├── migrations/ # Database migrations
├── README.md # This file
├── scripts/ # Utility scripts
│ └── init_db # Script to initialize the database
├── src/
│ ├── config/ # Configuration loading and management
│ │ ├── database.rs # Database connection settings
│ │ ├── environment.rs # Environment-related settings (e.g., enum for environment type)
│ │ └── application.rs # Application-specific settings
│ ├── domain/ # Defines the core business logic and data structures.
│ ├── errors/ # Custom error types
│ ├── lib.rs
│ ├── main.rs
│ ├── middleware/ # Custom Axum middleware
│ ├── models/ # Defines the database models (entities).
│ ├── repositories/ # Provides an abstraction layer for data access.
│ ├── routes/ # Defines the Axum route handlers.
│ ├── services/ # Implements the business logic of the application.
│ └── startup.rs # Application startup and configuration
├── templates/ # Askama templates
├── tests/ # Integration tests
│ └── api/ # API-specific tests
│ ├── health_check.rs # Tests for the health check endpoint
│ └── helpers.rs # Helper functions for API tests
Key Components
config: Handles loading and merging configuration files.domain: Defines the core business logic and data structures.models: Defines the database models (entities).repositories: Provides an abstraction layer for data access.services: Implements the business logic of the application.routes: Defines the Axum route handlers.telemetry: Sets up tracing and logging.templates: Contains the Askama templates.
Contributing
Contributions are welcome! Please submit a pull request with your changes.
License
This project is licensed under the MIT License.