A DDD-style Rust server with Axum, JWT authentication, and role-based access control.
- Domain-Driven Design (DDD) architecture
- Axum web framework
- JWT authentication
- Role-based access control
- PostgreSQL database integration
- In-memory repository for testing
- Docker support for easy deployment
- CORS configuration
- Tracing for logging and monitoring
- Environment variables for configuration
The project follows a DDD architecture with the following layers:
- Domain: Core business logic, entities, and repository interfaces
- Application: Use cases and application services
- Infrastructure: Technical implementations of interfaces defined in the domain
- App: Entry point and configuration
- Rust (latest stable version)
- PostgreSQL
- Docker (optional, for running PostgreSQL)
- Clone the repository:
git clone https://github.com/yourusername/rust-basic-server.git
cd rust-basic-server- Run the application using Docker Compose:
# Using the provided script
./scripts/run_docker.sh
# Or manually
docker-compose up -dThis will start:
- The main application with PostgreSQL repository on port 8080
- A version of the application with in-memory repository on port 8081
- A PostgreSQL database
- To stop the containers:
# Using the provided script
./scripts/stop_docker.sh
# Or manually
docker-compose down- Clone the repository:
git clone https://github.com/yourusername/rust-basic-server.git
cd rust-basic-server- Set up the database:
# Using Docker
docker run --name postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -e POSTGRES_DB=rust_server -p 5432:5432 -d postgres
# Or use your existing PostgreSQL installation
createdb rust_server- Configure environment variables:
Copy the .env.example file to .env and adjust the values as needed:
cp .env.example .env- Run the server:
cargo runThe server uses JWT for authentication. To authenticate:
- Register a new user:
curl -X POST http://localhost:8080/api/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"admin","email":"admin@example.com","password":"password","role":"Admin"}'- Login to get a JWT token:
curl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"password"}'- Use the token in subsequent requests:
curl -X GET http://localhost:8080/api/users \
-H "Authorization: Bearer YOUR_JWT_TOKEN"The server supports the following roles:
- Admin: Full access to all endpoints
- Manager: Access to user management (except deleting users)
- User: Access to own user data
- Guest: Limited access to public endpoints
cargo testcargo build --releaseThis project is licensed under the MIT License - see the LICENSE file for details.