Skip to content

Commit 6e22186

Browse files
Add documentation (#11)
* Add documentation * Remove outdated run instruction from README.md
1 parent 5ffc79b commit 6e22186

File tree

8 files changed

+1324
-20
lines changed

8 files changed

+1324
-20
lines changed

.github/copilot-instructions.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# Python Template Server - AI Agent Instructions
2+
3+
## Project Overview
4+
5+
FastAPI-based template server providing reusable infrastructure for building secure HTTPS applications.
6+
Implements authentication, rate limiting, security headers, and observability foundations via a base `TemplateServer` class.
7+
Developers extend `TemplateServer` to create application-specific servers (see `ExampleServer` in `main.py`).
8+
9+
## Architecture & Key Components
10+
11+
### Application Factory Pattern
12+
13+
- Entry: `main.py:run()` → instantiates `ExampleServer` (subclass of `TemplateServer`) → calls `.run()`
14+
- `TemplateServer.__init__()` sets up middleware, rate limiting, metrics, and calls `setup_routes()`
15+
- **Critical**: Middleware order matters - request logging → security headers → rate limiting
16+
- **Extensibility**: Subclasses implement `setup_routes()` to add custom endpoints and `validate_config()` for config validation
17+
18+
### Configuration System
19+
20+
- `config.json` loaded via `TemplateServer.load_config()` method
21+
- Validated using Pydantic models in `models.py` (TemplateServerConfig hierarchy)
22+
- Subclasses override `validate_config()` to provide custom config models
23+
- Logging configured automatically on `logging_setup.py` import with rotating file handler
24+
- Environment variables stored in `.env` (API_TOKEN_HASH only, never commit)
25+
26+
### Authentication Architecture
27+
28+
- **Token Generation**: `uv run generate-new-token` creates secure token + SHA-256 hash
29+
- **Hash Storage**: Only hash stored in `.env` (API_TOKEN_HASH), raw token shown once
30+
- **Token Loading**: `load_hashed_token()` loads hash from .env on server startup, stored in `TemplateServer.hashed_token`
31+
- **Verification Flow**: Request → `_verify_api_key()` dependency → `verify_token()` → hash comparison
32+
- **Metrics**: Success/failure counters with labeled reasons (missing/invalid/error)
33+
- **Health Endpoint**: `/api/health` does NOT require authentication, reports unhealthy if token not configured
34+
- Header: `X-API-Key` (defined in `constants.API_KEY_HEADER_NAME`)
35+
36+
### Rate Limiting
37+
38+
- Uses `slowapi` with configurable storage (in-memory/Redis/Memcached)
39+
- Applied via `_limit_route()` wrapper when `config.rate_limit.enabled=true`
40+
- Custom exception handler increments `rate_limit_exceeded_counter` per endpoint
41+
- Format: `"100/minute"` (supports /second, /minute, /hour)
42+
43+
### Observability Stack
44+
45+
- **Prometheus**: `/metrics` endpoint always exposed (no auth), custom auth/rate-limit metrics
46+
- **Grafana**: Pre-configured dashboards in `grafana/dashboards/*.json`
47+
- **Logging**: Dual output (console + rotating file), 10MB per file, 5 backups in `logs/`
48+
- **Request Tracking**: `RequestLoggingMiddleware` logs all requests with client IP
49+
50+
## Developer Workflows
51+
52+
### Essential Commands
53+
54+
```powershell
55+
# Setup (first time)
56+
uv sync # Install dependencies
57+
uv run generate-certificate # Create self-signed SSL certs (certs/ dir)
58+
uv run generate-new-token # Generate API key, save hash to .env
59+
60+
# Development
61+
uv run python-template-server # Start server (https://localhost:443/api)
62+
uv run -m pytest # Run tests with coverage
63+
uv run -m mypy . # Type checking
64+
uv run -m ruff check . # Linting
65+
66+
# Docker Development
67+
docker compose up --build -d # Build + start all services
68+
docker compose logs -f python-template-server # View logs
69+
docker compose down # Stop and remove containers
70+
```
71+
72+
### Testing Patterns
73+
74+
- **Fixtures**: All tests use `conftest.py` fixtures, auto-mock `pyhere.here()` to tmp_path
75+
- **Config Mocking**: Use fixtures for consistent test config
76+
- **Integration Tests**: Test via FastAPI TestClient with auth headers
77+
- **Coverage Target**: 99% (currently achieved)
78+
- **Pattern**: Unit tests per module (test\_\*.py) + integration tests (test_template_server.py)
79+
80+
### Docker Multi-Stage Build
81+
82+
- **Stage 1 (builder)**: Uses `uv` to build wheel, copies `configuration/` directory and other required files
83+
- **Stage 2 (runtime)**: Installs wheel, copies runtime files (.here, configs, LICENSE, README.md) from wheel to /app
84+
- **Startup Script**: `/app/start.sh` generates token/certs if missing, starts server
85+
- **Config Selection**: Uses `config.json` for all environments
86+
- **Build Args**: `PORT=443` (exposes port)
87+
- **Health Check**: Curls `/api/health` with unverified SSL context (no auth required)
88+
- **User**: Switches to non-root user `templateserver` (UID 1000)
89+
90+
## Project-Specific Conventions
91+
92+
### Code Organization
93+
94+
- **Handlers**: Separate modules for auth (`authentication_handler.py`), certs (`certificate_handler.py`)
95+
- **Middleware**: Dedicated package `middleware/` with base classes extending `BaseHTTPMiddleware`
96+
- **Constants**: All magic strings/numbers in `constants.py` (ports, file names, log config)
97+
- **Models**: Pydantic models for config + API responses, use `@property` for derived values
98+
99+
### Security Patterns
100+
101+
- **Never log secrets**: Print tokens via `print()`, not `logger` (see `generate_new_token()`)
102+
- **Path validation**: Use Pydantic validators, Path objects for cert paths
103+
- **Security headers**: HSTS, CSP, X-Frame-Options via `SecurityHeadersMiddleware`
104+
- **Cert generation**: RSA-4096, SHA-256, 365-day validity, SANs for localhost
105+
106+
### API Design
107+
108+
- **Prefix**: All routes under `/api` (API_PREFIX constant)
109+
- **Authentication**: Applied via `dependencies=[Security(self._verify_api_key)]` in route registration
110+
- **Unauthenticated Endpoints**: `/health` and `/metrics` do not require authentication
111+
- **Response Models**: All endpoints return `BaseResponse` subclasses with code/message/timestamp
112+
- **Health Status**: `/health` includes `status` field (HEALTHY/DEGRADED/UNHEALTHY), reports unhealthy if no token configured
113+
114+
### Logging Format
115+
116+
- Format: `[DD/MM/YYYY | HH:MM:SS] (LEVEL) module: message`
117+
- Client IPs logged in requests: `"Request: GET /api/health from 192.168.1.1"`
118+
- Auth failures: `"Invalid API key attempt!"`
119+
120+
## Development Constraints
121+
122+
### What's NOT Implemented Yet
123+
124+
- Custom domain-specific endpoints (template provides base functionality only)
125+
- Database/metadata storage (users implement as needed in subclasses)
126+
- CORS configuration (can be added by subclasses)
127+
- API key rotation/expiry
128+
- Multi-user auth (JWT/OAuth2)
129+
130+
### Testing Requirements
131+
132+
- Mock `pyhere.here()` for all file path tests (see `conftest.py`)
133+
- Use fixtures for TemplateServer/ExampleServer instantiation
134+
- Test async endpoints with `@pytest.mark.asyncio`
135+
- Mock `uvicorn.run` when testing server `.run()` methods
136+
137+
### CI/CD Validation
138+
139+
All PRs must pass:
140+
141+
**CI Workflow:**
142+
143+
1. `validate-pyproject` - pyproject.toml schema validation
144+
2. `ruff` - linting (120 char line length, strict rules in pyproject.toml)
145+
3. `mypy` - 100% type coverage (strict mode)
146+
4. `pytest` - 99% code coverage, HTML report uploaded
147+
5. `version-check` - pyproject.toml vs uv.lock version consistency
148+
149+
**Docker Workflow:**
150+
151+
1. `docker-development` - Build and test dev image with docker compose
152+
2. `docker-production` - Build and test prod image with ENV=prod, PORT=443
153+
154+
## Quick Reference
155+
156+
### Key Files
157+
158+
- `template_server.py` - Base TemplateServer class with middleware/metrics/auth setup
159+
- `main.py` - ExampleServer implementation showing how to extend TemplateServer
160+
- `authentication_handler.py` - Token generation, hashing, verification
161+
- `logging_setup.py` - Logging configuration (executed on import)
162+
- `models.py` - All Pydantic models (config + responses)
163+
- `constants.py` - Project constants, logging config
164+
- `docker-compose.yml` - FastAPI + Prometheus + Grafana stack
165+
166+
### Environment Variables
167+
168+
- `API_TOKEN_HASH` - SHA-256 hash of API token (only var required)
169+
170+
### Configuration Files
171+
172+
- `configuration/config.json` - Configuration (used for all environments)
173+
- `.env` - API token hash (auto-created by generate-new-token)
174+
- **Docker**: Startup script uses config.json for all environments

README.md

Lines changed: 108 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,128 @@
11
[![python](https://img.shields.io/badge/Python-3.12-3776AB.svg?style=flat&logo=python&logoColor=ffd343)](https://docs.python.org/3.12/)
22
[![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json)](https://github.com/astral-sh/uv)
33
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
4+
[![FastAPI](https://img.shields.io/badge/FastAPI-Latest-009688?style=flat&logo=fastapi&logoColor=white)](https://fastapi.tiangolo.com/)
45
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
56

67
<!-- omit from toc -->
7-
# Template Python Repository
8-
This repository can be used as a template for a Python application.
8+
# Python Template Server
9+
10+
A production-ready FastAPI server template with built-in authentication, rate limiting, security headers, and Prometheus metrics. This repository provides a solid foundation for building secure, observable FastAPI applications.
911

1012
<!-- omit from toc -->
1113
## Table of Contents
12-
- [uv](#uv)
13-
- [Installing Dependencies](#installing-dependencies)
14-
- [Testing, Linting, and Type Checking](#testing-linting-and-type-checking)
14+
- [Features](#features)
15+
- [Architecture](#architecture)
16+
- [Quick Start](#quick-start)
17+
- [Prerequisites](#prerequisites)
18+
- [Installation](#installation)
19+
- [Generate Certificates and API Token](#generate-certificates-and-api-token)
20+
- [Run the Server](#run-the-server)
21+
- [Using as a Template](#using-as-a-template)
22+
- [Docker Deployment](#docker-deployment)
23+
- [Documentation](#documentation)
24+
25+
## Features
26+
27+
- **TemplateServer Base Class**: Reusable foundation
28+
- **FastAPI Framework**: Modern, fast, async-ready web framework
29+
- **Observability Stack**: Pre-configured Prometheus + Grafana dashboards
30+
- **Docker Support**: Multi-stage builds with docker-compose orchestration
31+
- **Production Patterns**: Token generation, SSL certificate handling, health checks
32+
33+
## Architecture
34+
35+
This project uses a **`TemplateServer` base class** that encapsulates cross-cutting concerns:
36+
37+
- **Request Logging**: All requests/responses logged with client IP tracking
38+
- **Security Headers**: HSTS/CSP/X-Frame-Options automatically applied
39+
- **API Key Verification**: SHA-256 hashed tokens with secure validation
40+
- **Rate Limiting**: Configurable limits using `slowapi` (in-memory/Redis/Memcached)
41+
- **Prometheus Metrics**: Custom authentication/rate-limit metrics + HTTP instrumentation
42+
43+
**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.
44+
45+
## Quick Start
1546

16-
## uv
17-
This repository is managed using the `uv` Python project manager: https://docs.astral.sh/uv/
47+
### Prerequisites
1848

19-
To install `uv`:
49+
- Python 3.12+
50+
- [uv](https://docs.astral.sh/uv/) package manager
51+
52+
Install `uv`:
2053

2154
```sh
22-
curl -LsSf https://astral.sh/uv/install.sh | sh # Linux/Mac
23-
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex" # Windows
55+
# Linux/Mac
56+
curl -LsSf https://astral.sh/uv/install.sh | sh
57+
58+
# Windows (PowerShell)
59+
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
2460
```
2561

26-
## Installing Dependencies
27-
Install the required dependencies using `uv`:
62+
### Installation
63+
64+
```sh
65+
# Clone the repository
66+
git clone https://github.com/javidahmed64592/python-template-server.git
67+
cd python-template-server
68+
69+
# Install dependencies
70+
uv sync --extra dev
71+
```
2872

29-
uv sync
73+
### Generate Certificates and API Token
3074

31-
To install with `dev` dependencies:
75+
```sh
76+
# Generate self-signed SSL certificate (saves to certs/ directory)
77+
uv run generate-certificate
3278

33-
uv sync --extra dev
79+
# Generate API authentication token (saves hash to .env)
80+
uv run generate-new-token
81+
# ⚠️ Save the displayed token - you'll need it for API requests!
82+
```
83+
84+
### Run the Server
85+
86+
```sh
87+
# Start the server
88+
uv run python-template-server
89+
90+
# Server runs at https://localhost:443/api
91+
# Health check: curl -k https://localhost:443/api/health
92+
# Metrics: curl -k https://localhost:443/api/metrics
93+
```
94+
95+
## Using as a Template
96+
97+
To create your own server:
98+
99+
1. **Create a subclass of `TemplateServer`** (see `python_template_server/main.py:ExampleServer` as reference)
100+
2. **Implement required methods**:
101+
- `validate_config()`: Validate your config model
102+
- `setup_routes()`: Define your API endpoints
103+
3. **Add custom routes** using FastAPI decorators on `self.app`
104+
4. **Configure** via `configuration/config.json`
105+
106+
See the [Software Maintenance Guide](./docs/SMG.md) for detailed setup instructions.
107+
108+
## Docker Deployment
109+
110+
```sh
111+
# Start all services (FastAPI + Prometheus + Grafana)
112+
docker compose up -d
113+
114+
# View logs
115+
docker compose logs -f python-template-server
116+
117+
# Access services:
118+
# - API: https://localhost:443/api
119+
# - Prometheus: http://localhost:9090
120+
# - Grafana: http://localhost:3000 (admin/admin)
121+
```
34122

35-
## Testing, Linting, and Type Checking
123+
## Documentation
36124

37-
- **Run tests:** `uv run pytest`
38-
- **Lint code:** `uv run ruff check .`
39-
- **Format code:** `uv run ruff format .`
40-
- **Type check:** `uv run mypy .`
125+
- **[API Documentation](./docs/API.md)**: Endpoints, authentication, metrics
126+
- **[Software Maintenance Guide](./docs/SMG.md)**: Development setup, configuration
127+
- **[Docker Deployment Guide](./docs/DOCKER_DEPLOYMENT.md)**: Container orchestration
128+
- **[Workflows](./docs/WORKFLOWS.md)**: CI/CD pipeline details

0 commit comments

Comments
 (0)