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