A production-ready FastAPI server template with built-in authentication, rate limiting and security headers. This repository provides a solid foundation for building secure, observable FastAPI applications.
- TemplateServer Base Class: Reusable foundation with middleware, auth, and rate limiting
- FastAPI Framework: Modern, fast, async-ready web framework
- CORS Support: Configurable cross-origin resource sharing for frontend integration
- Static File Serving: FastAPI's StaticFiles mounting with custom 404.html support
- Docker Support: Multi-stage builds with docker-compose orchestration
- Production Patterns: Token generation, SSL certificate handling, health checks
This project uses a TemplateServer base class that encapsulates cross-cutting concerns:
- Request Logging: All requests/responses logged with client IP tracking
- Security Headers: HSTS/CSP/X-Frame-Options automatically applied
- API Key Verification: SHA-256 hashed tokens with secure validation
- Rate Limiting: Configurable limits using
slowapi(in-memory/Redis/Memcached) - CORS Middleware: Optional cross-origin support for frontend applications
- Static File Serving: Serve SPAs or static assets using FastAPI's StaticFiles mounting
Application-specific servers (like ExampleServer in main.py) extend TemplateServer to implement domain-specific endpoints and business logic. The base class handles all infrastructure concerns, letting you focus on your API functionality.
Download the latest release from GitHub Releases.
Rename .env.example to .env and edit it to configure the server.
HOST: Server host address (default: localhost)PORT: Server port (default: 443)API_TOKEN_HASH: Leave blank to auto-generate on first run, or provide your own token hash
# Start the container
docker compose up -d
# Stop the container
docker compose down
# Update to the latest version
docker compose pull && docker compose up -d
# View the logs
docker compose logs -f python-template-serverNote: You may need to add your user to the Docker group and log out/in for permission changes to take effect:
sudo usermod -aG docker ${USER}To create your own server:
- Create a subclass of
TemplateServer(seepython_template_server/main.py:ExampleServeras reference) - Implement required methods:
validate_config(): Validate your config modelsetup_routes(): Define your API endpoints
- Add custom routes using FastAPI decorators on
self.app - Configure via
configuration/config.json
See the Software Maintenance Guide for detailed setup instructions.
- API Documentation: API architecture and endpoints
- Software Maintenance Guide: Development setup, configuration
- Workflows: CI/CD pipeline details
This project is licensed under the MIT License - see the LICENSE file for details.