Skip to content

bitnob/rust-webservice-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Rust Web Services Starter Template

A production-ready, enterprise-grade Rust web services starter template with domain-driven design, comprehensive security, and first-class idempotency support.

πŸš€ Features

βœ… Phase 1: Core Infrastructure (COMPLETED)

  • Domain-Driven Design: Clean architecture with clear separation of concerns
  • Axum Web Framework: Production-ready HTTP server with middleware support
  • Configuration Management: Environment-based configuration with validation
  • Structured Logging: JSON/Pretty logging with tracing support
  • Error Handling: Comprehensive error types with HTTP response mapping
  • Health Checks: Ready/Live endpoints for monitoring
  • Docker Support: Multi-stage Dockerfiles and docker-compose setup

πŸ”„ Upcoming Phases

  • Phase 2: Security & Authentication + Idempotency Framework
  • Phase 3: Advanced Features (Circuit Breakers, Message Queues, Observability)
  • Phase 4: Developer Experience (Hot Reload, Coverage, API Client Generation, Feature Flags)
  • Phase 5: Testing & Documentation
  • Phase 6: Additional Framework Support (Actix-Web)

πŸ“ Project Structure

src/
β”œβ”€β”€ main.rs                          # Application entry point
β”œβ”€β”€ lib.rs                           # Library exports
β”œβ”€β”€ config/                          # Configuration management
β”‚   β”œβ”€β”€ app_config.rs               # Application settings
β”‚   β”œβ”€β”€ database.rs                 # Database configuration
β”‚   └── observability.rs            # Metrics/tracing config
β”œβ”€β”€ infrastructure/                  # External integrations
β”‚   β”œβ”€β”€ database/                   # Database abstraction
β”‚   β”‚   └── traits.rs               # Repository traits
β”‚   └── observability/              # Metrics and tracing
β”‚       └── tracing.rs              # Tracing setup
β”œβ”€β”€ application/                     # Application services
β”‚   β”œβ”€β”€ services/                   # Business logic services
β”‚   └── dto/                        # Data transfer objects
β”œβ”€β”€ domain/                         # Core business logic
β”‚   β”œβ”€β”€ entities/                   # Domain entities
β”‚   β”œβ”€β”€ repositories/               # Repository traits
β”‚   └── errors/                     # Domain errors
β”œβ”€β”€ presentation/                   # HTTP layer
β”‚   β”œβ”€β”€ handlers/                   # HTTP handlers
β”‚   β”‚   └── health_handlers.rs      # Health check endpoints
β”‚   β”œβ”€β”€ middleware/                 # HTTP middleware
β”‚   β”‚   β”œβ”€β”€ correlation_id.rs       # Request correlation
β”‚   β”‚   └── logging_middleware.rs   # Request/response logging
β”‚   └── frameworks/                 # Framework implementations
β”‚       └── axum_impl.rs            # Axum server implementation
└── shared/                         # Shared utilities
    β”œβ”€β”€ errors.rs                   # Global error types
    β”œβ”€β”€ constants.rs                # Application constants
    β”œβ”€β”€ utils.rs                    # Utility functions
    └── types.rs                    # Common type definitions

πŸ› οΈ Technology Stack

  • Web Framework: Axum (with Actix-Web support planned)
  • Async Runtime: Tokio
  • Database: SQLx with PostgreSQL (MySQL, SQLite support planned)
  • Serialization: Serde
  • Logging: Tracing with structured output
  • Configuration: Config crate with environment variable support
  • Security: JWT, Argon2, input validation
  • Observability: Prometheus metrics, OpenTelemetry integration
  • Testing: Comprehensive unit, integration, and smoke tests

πŸš€ Quick Start

Prerequisites

  • Rust 1.75+
  • Docker and Docker Compose
  • PostgreSQL (or use Docker)

1. Clone and Setup

git clone <repository-url>
cd rust_webservices_starter
cp .env.example .env.local

2. Configure Environment

Edit .env.local with your settings:

# Server
APP_SERVER__HOST=0.0.0.0
APP_SERVER__PORT=8080

# Database
APP_DATABASE__HOST=localhost
APP_DATABASE__USERNAME=postgres
APP_DATABASE__PASSWORD=password
APP_DATABASE__DATABASE=app_db

# JWT Secret (minimum 32 characters)
APP_AUTH__JWT_SECRET=your-super-secret-jwt-key-change-this-in-production-minimum-32-characters

3. Run with Docker Compose

# Start all services (app, postgres, redis, prometheus, grafana)
docker-compose up -d

# View logs
docker-compose logs -f app

4. Run Locally

# Start dependencies
docker-compose up postgres redis -d

# Run the application
cargo run --bin server

5. Test the Service

# Health check
curl http://localhost:8080/health

# Readiness check (includes dependency health)
curl http://localhost:8080/health/ready

# Liveness check
curl http://localhost:8080/health/live

πŸ“Š Monitoring

πŸ”§ Development

Build

cargo build

Test

cargo test

Check

cargo check

Format

cargo fmt

Lint

cargo clippy

πŸ“ Configuration

The application uses environment-based configuration with the following sources (in order of precedence):

  1. Environment variables with APP_ prefix
  2. config/local.toml (for local overrides)
  3. config/{environment}.toml (development/staging/production)
  4. config/default.toml (defaults)

Key Configuration Sections

  • Server: Host, port, timeouts, connection limits
  • Database: Connection details, pool configuration
  • Authentication: JWT settings, token expiration
  • Observability: Logging format, metrics, tracing
  • Feature Flags: Enable/disable specific features

πŸ”’ Security Features

  • CORS: Configurable cross-origin resource sharing
  • Request Logging: Comprehensive request/response logging with correlation IDs
  • Error Handling: Secure error responses without information leakage
  • Input Validation: Built-in validation framework
  • Security Headers: Standard security headers applied

πŸ—οΈ Architecture Principles

Domain-Driven Design

  • Domain Layer: Core business logic and entities
  • Application Layer: Use cases and application services
  • Infrastructure Layer: External integrations and persistence
  • Presentation Layer: HTTP handlers and framework-specific code

Dependency Direction

Presentation β†’ Application β†’ Domain
     ↓              ↓
Infrastructure ← ← ← ←

Key Patterns

  • Repository Pattern: Abstract data access
  • Dependency Injection: Clean separation of concerns
  • Error Propagation: Comprehensive error handling
  • Configuration: Environment-based settings

πŸ§ͺ Testing Strategy

# Unit tests
cargo test --lib

# Integration tests
cargo test --test integration

# All tests
cargo test

🀝 Contributing

  1. Follow the established project structure
  2. Add comprehensive tests for new features
  3. Update documentation for significant changes
  4. Ensure all checks pass: cargo check, cargo test, cargo clippy

πŸ“„ License

MIT License - see LICENSE file for details

πŸ—ΊοΈ Roadmap

Phase 2: Security & Authentication (Next)

  • JWT authentication middleware
  • Input validation and sanitization
  • Rate limiting implementation
  • Idempotency framework with Redis/Database storage

Phase 3: Advanced Features

  • Message queue abstraction (SQS, RabbitMQ, Redis Streams)
  • Circuit breaker with jitter
  • Comprehensive observability (Prometheus metrics)
  • Advanced health checks

Phase 4: Developer Experience

  • Hot reload development server
  • Code coverage integration
  • OpenAPI schema generation
  • API client generation (TypeScript, Python, Rust)
  • Built-in feature flags system

Phase 5: Testing & Documentation

  • Comprehensive test suite
  • CI/CD pipeline
  • Interactive setup script
  • Complete documentation

Phase 6: Framework Support

  • Actix-Web implementation
  • Alternative database drivers
  • Additional message queue providers

πŸ“ž Support

For questions, issues, or contributions, please visit our GitHub repository.

About

A starter template for webservices built in rust

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages