A state-of-the-art FastAPI application designed to provide authentication and authorization services. Built with deployment to Kubernetes in mind.
The application follows a layered architecture:
app/: Main application packagemain.py: FastAPI application entry pointapi/: API routes and endpointscore/: Core functionality (config, security, health)
- FastAPI application with modern Python 3.11+
- JWT Authentication
- Health check endpoints for Kubernetes probes
- Dockerized application with multi-stage builds
- Process management with Gunicorn and Uvicorn
-
Authentication
POST /api/v1/auth/token: Get access tokenGET /api/v1/auth/me: Get current user info
-
Health Checks
GET /health: General health check (used by Docker healthcheck)GET /readiness: Kubernetes readiness probeGET /liveness: Kubernetes liveness probe
-
API Documentation
/api/v1/docs: Swagger UI (interactive API testing)/api/v1/redoc: ReDoc (clean documentation reading)
# Start the development server with live code reloading
docker-compose up
# Start in detached mode
docker-compose up -d
# View logs
docker-compose logs -f
# Access API docs at http://localhost:8000/api/v1/docsThe development configuration:
- Maps your local code into the container (changes apply instantly)
- Automatically reloads when code changes
- Includes development environment variables
# Rebuild and restart the container
docker-compose down
docker-compose build
docker-compose up -d# Run with production configuration (default 2 gunicorn workers)
SECRET_KEY=yoursecretkey docker-compose -f docker-compose.prod.yml up
# Run with custom number of workers
GUNICORN_WORKERS=4 SECRET_KEY=yoursecretkey docker-compose -f docker-compose.prod.yml up| Variable | Description | Default |
|---|---|---|
ENVIRONMENT |
Environment name (dev, staging, production) | dev |
SECRET_KEY |
Secret key for JWT token generation | Random in dev |
ACCESS_TOKEN_EXPIRE_MINUTES |
Token expiration time | 30 |
LOG_LEVEL |
Logging level | INFO |
CORS_ORIGINS |
CORS allowed origins (comma-separated) | Local URLs |
GUNICORN_WORKERS |
Number of Gunicorn workers (production only) | 2 |
The application includes:
- Health check endpoints for liveness and readiness probes
- Resource specification in Docker Compose
- Container optimization for Kubernetes
# Stop containers
docker-compose down
# Remove MySQL volume
docker volume rm auth_service_mysql_data
# Start containers with fresh data
docker-compose up -dThe project includes a custom SQL migration system that tracks executed migrations in a migrations table.
# View available migration files
python migrations/run.py --list
# Run all pending migrations
python migrations/run.py
# Run a specific migration (without .sql extension)
python migrations/run.py --file 001_create_buildings_tableTo add a new migration:
- Create a new SQL file in the
migrations/sql/directory with a numbered prefix (e.g.,002_add_users_table.sql) - Write your SQL statements, ending each with a semicolon (;)
- Run the migration script