Lightweight API framework for Cloudflare Python Workers - like Starlette but for Workers
Project description
flarelette
Lightweight API framework for Cloudflare Python Workers - like Starlette but for Workers.
Version: 0.1.2
Features
- Flask-style routing with decorators
- Schema-based request validation (no pydantic)
- Structured JSON logging
- Middleware support
- Security headers by default
- Type-safe with full type hints
- Single dependency:
python-json-logger(2KB)
Quick Start
from flarelette import App, Request, JsonResponse, Field, validate_body
from flarelette.logging import StructuredLogger, LoggingMiddleware
# Initialize app
app = App()
logger = StructuredLogger("my-service")
# Add logging middleware
app.use(LoggingMiddleware(logger))
# Define routes with validation
@app.route("/health", methods=["GET"])
async def health(request: Request) -> JsonResponse:
return JsonResponse({"status": "healthy"})
@app.route("/api/data", methods=["POST"])
@validate_body({
"name": Field(type=str, required=True, min_length=1),
"value": Field(type=float, required=True, min_value=0),
"category": Field(type=str, required=False, enum=["A", "B", "C"]),
})
async def process_data(request: Request) -> JsonResponse:
data = await request.json() # Already validated
return JsonResponse({"result": "success"})
# Error handling
@app.error_handler
async def handle_error(error: Exception) -> JsonResponse:
logger.error("Unhandled error", error=str(error))
return JsonResponse(
{"error": "Internal Server Error"},
status=500
)
# Export for Cloudflare Workers
async def on_fetch(request):
response = await app.handle(request)
return response.to_workers_response()
Components
| Component | Purpose |
|---|---|
App |
Route registration and handling |
Router |
Route matching |
Request |
Request wrapper with validation |
Response |
Response builder with security |
Field |
Validation rules |
validate_body |
Request validation decorator |
StructuredLogger |
JSON logging (ADR-0013) |
LoggingMiddleware |
Request/response logging |
HttpError hierarchy |
Type-safe error handling |
Validation
Field Rules
Field(
type=str|int|float|bool|list|dict, # Required: expected type
required=True|False, # Default: True
min_length=int, # For strings, lists, dicts
max_length=int, # For strings, lists, dicts
min_value=int|float, # For numeric types
max_value=int|float, # For numeric types
enum=list, # Allowed values
pattern=str, # Regex pattern (for strings)
)
Example
@app.route("/calculate", methods=["POST"])
@validate_body({
"settlementDate": Field(type=str, pattern=r"^\d{4}-\d{2}-\d{2}$"),
"couponRate": Field(type=float, min_value=0, max_value=1),
"frequency": Field(type=int, enum=[1, 2, 4, 12]),
"pairs": Field(type=list, min_length=1),
"options": Field(type=dict, required=False),
})
async def calculate(request: Request) -> JsonResponse:
body = await request.json() # Already validated
return JsonResponse({"result": "success"})
Validation errors return 422 with descriptive messages.
Security
- Security headers:
X-Content-Type-Options,X-Frame-Options,X-XSS-Protection - Schema validation with type checking
- Safe error messages (no stack traces in responses)
- Structured error logging
Testing
pytest # Run tests
pytest --cov # Coverage
mypy src/flarelette # Type check
ruff check src # Lint
black src # Format
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
flarelette-0.1.2.tar.gz
(20.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flarelette-0.1.2.tar.gz.
File metadata
- Download URL: flarelette-0.1.2.tar.gz
- Upload date:
- Size: 20.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5aa6e0221f4936d0321c51ecf13020cc5aae2d281e0b4a0ab37634971257aa7c
|
|
| MD5 |
25702205a502e31283f7bf02fd20a3b8
|
|
| BLAKE2b-256 |
a25aa91dac2fbcae35dc9312c42a9ae407048631d262bd2cd033442d4c6bb172
|
File details
Details for the file flarelette-0.1.2-py3-none-any.whl.
File metadata
- Download URL: flarelette-0.1.2-py3-none-any.whl
- Upload date:
- Size: 17.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee82b475416e26cb0fbf17b2968e60bee507c054b1dd7fce6b3a1aafd7925bc1
|
|
| MD5 |
2846536029d676875fbe8c7dc14f571a
|
|
| BLAKE2b-256 |
af6a3718fb1bc24a594ad69fe5d8f2392f5f188a73e80769596063c8b1d4b4a0
|