Professional REST API template for GitLab projects using FastAPI, SQLAlchemy, and PostgreSQL.
- FastAPI - Modern, fast web framework
- SQLAlchemy - Async ORM for database operations
- PostgreSQL - Robust relational database
- Redis - Caching and session management
- Celery - Background task processing
- JWT Authentication - Secure token-based auth
- Pydantic - Data validation and settings
- Alembic - Database migrations
- Typer CLI - Powerful command-line interface
- Webhooks - Webhook handlers structure
- Docker - Containerization support
- CI/CD - GitLab pipeline included
- Pre-commit - Automated code quality checks
- Testing - Comprehensive test suite (70%+ coverage)
- Type Hints - Full type coverage
- OpenAPI - Auto-generated documentation
.
βββ app/ # Application source code
β βββ api/ # API endpoints
β β βββ v1/ # API version 1
β β βββ routes/ # API route handlers
β β βββ webhooks/ # Webhook handlers
β βββ config/ # Configuration (settings + JSON files)
β βββ core/ # Core functionality (DB, cache, security)
β βββ models/ # SQLAlchemy models
β βββ schemas/ # Pydantic schemas
β βββ services/ # Business logic layer
β βββ repositories/ # Data access layer
β βββ tasks/ # Celery background tasks
β βββ middleware/ # Custom middleware
β βββ utils/ # Utility functions
β βββ cli.py # CLI commands
β βββ main.py # Application entry point
βββ tests/ # Test suite
β βββ unit/ # Unit tests
β βββ integration/ # Integration tests
βββ docs/ # Documentation
βββ alembic/ # Database migrations
βββ .gitlab/ # GitLab templates
β βββ issue_templates/ # Issue templates
β βββ merge_request_templates/ # MR templates
βββ .gitlab-ci.yml # CI/CD pipeline
βββ Dockerfile # Container definition
βββ docker-compose.yml # Multi-container setup
βββ requirements.txt # Python dependencies
- Python 3.11+
- PostgreSQL 15+
- Redis 7+
- Git
- Clone repository
git clone <repository-url>
cd rest-python-backend-boilerplate- Create virtual environment
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate- Install dependencies & setup
make setup
# Or manually:
# pip install -r requirements.txt -r requirements-dev.txt
# pre-commit install
# git config commit.template .gitmessage- Configure environment
cp .env.example .env
# Edit .env with your configuration- Run migrations
alembic upgrade head- Start server
python -m app.main
# Or use CLI
python -m app.cli run-serverVisit:
- API: http://localhost:8000
- Docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
docker-compose up -d# Run server
python -m app.cli run-server --reload
# Database operations
python -m app.cli init-db
python -m app.cli run-migrations
python -m app.cli create-migration "Add new field"
# Create superuser
python -m app.cli create-superuser
# Run Celery worker
python -m app.cli run-worker
# Run Celery beat
python -m app.cli run-beat
# Code quality
python -m app.cli lint
python -m app.cli format-code
python -m app.cli testgit config commit.template .gitmessage# Install pre-commit hooks
pre-commit install
# Run manually on all files
pre-commit run --all-files# All tests
pytest
# With coverage
pytest --cov=src --cov-report=html
# Specific tests
pytest tests/unit/test_security.py -v# Format code
black app/ tests/
isort app/ tests/
# Lint
ruff check app/ tests/
# Type check
mypy app/
# Or use CLI
python -m app.cli format-code
python -m app.cli lint# Create migration
alembic revision --autogenerate -m "description"
# Apply migrations
alembic upgrade head
# Rollback
alembic downgrade -1- JWT token authentication
- Password hashing with bcrypt
- CORS configuration
- Environment variable management
- SQL injection protection (ORM)
pytest tests/unit/ -vpytest tests/integration/ -vpytest --cov=src --cov-report=html
open htmlcov/index.htmlRequired variables:
DATABASE_URL- PostgreSQL connection stringREDIS_URL- Redis connection stringSECRET_KEY- JWT secret key
docker build -t api:latest .
docker run -p 8000:8000 --env-file .env api:latestGitLab CI/CD pipeline includes:
- Lint - Code formatting, linting, type checking, security scans
- Test - Unit and integration tests on Python 3.10, 3.11, 3.12
- Build - Build and push Docker images
- Deploy - Deploy to staging/production (manual)
Features:
- Configurable inputs (coverage threshold, Python versions, etc.)
- Parallel test execution
- PostgreSQL + Redis services
- Coverage enforcement (70% minimum)
- Multiple Python version testing
See CI/CD Documentation for details.
- Setup development environment:
make setup - Create issue describing the change
- Create branch from
develop - Make changes following code style
- Pre-commit hooks run automatically
- Write/update tests (min 70% coverage)
- Submit merge request
- Wait for review and approval
See Contributing Guide for detailed workflow.
- β Code formatting (Black, isort)
- β Linting (Ruff)
- β Type checking (MyPy)
- β Security checks (Bandit)
- β Test coverage (min 70%)
- Bug Report
- Feature Request
- Task
- Description
- Related issues
- Type of change
- Testing checklist
- Deployment notes
Layered architecture:
- API Layer - Request/response handling
- Service Layer - Business logic
- Repository Layer - Data access
- Model Layer - Data models
Configuration via environment variables or .env file.
See .env.example for all options.
- Health check endpoint:
/api/v1/health - Structured logging (JSON format)
- Request/response logging
MIT License
Create an issue in GitLab for:
- Bug reports
- Feature requests
- Questions
- Documentation improvements