Skip to content

javidahmed64592/python-template-server

Repository files navigation

python uv Ruff FastAPI CI Build Docker License: MIT

Python Template Server

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.

Table of Contents

Features

  • TemplateServer Base Class: Reusable foundation
  • FastAPI Framework: Modern, fast, async-ready web framework
  • Docker Support: Multi-stage builds with docker-compose orchestration
  • Production Patterns: Token generation, SSL certificate handling, health checks

Architecture

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)

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.

Quick Start

Prerequisites

  • Python 3.13+
  • uv package manager

Install uv:

# Linux/Mac
curl -LsSf https://astral.sh/uv/install.sh | sh

# Windows (PowerShell)
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

Installation

# Clone the repository
git clone https://github.com/javidahmed64592/python-template-server.git
cd python-template-server

# Install dependencies
uv sync --extra dev

Generate API Token

uv run generate-new-token
# ⚠️ Save the displayed token - you'll need it for API requests!

Run the Server

# Start the server
uv run python-template-server

# Server runs at https://localhost:443/api
# Swagger UI: https://localhost:443/api/docs
# Redoc: https://localhost:443/api/redoc
# Health check: curl -k https://localhost:443/api/health
# Login (requires authentication): curl -k -H "X-API-Key: your-token-here" https://localhost:443/api/login

Using as a Template

To create your own server:

  1. Create a subclass of TemplateServer (see python_template_server/main.py:ExampleServer as reference)
  2. Implement required methods:
    • validate_config(): Validate your config model
    • setup_routes(): Define your API endpoints
  3. Add custom routes using FastAPI decorators on self.app
  4. Configure via configuration/config.json

See the Software Maintenance Guide for detailed setup instructions.

Documentation

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A template FastAPI server with authentication, rate limiting and Prometheus metrics.

Resources

License

Stars

Watchers

Forks

Packages