servekat library
Project description
ServeKat
FastAPI server framework with batteries included. Provides route auto-discovery, exception handling, API schemas, and middleware utilities for building production-ready APIs.
Features
- Route Auto-Discovery: Automatically discover and mount routers from packages
- API Schema Base Classes: ResponseSchema, CreateSchema, UpdateSchema, PaginatedResponse
- Exception Handling: Automatic CoreKat exception → HTTP response mapping
- Middleware Stack: CORS, error handling, metrics, request timing
- Auth System: Token-based authentication with role-based access control
- Health Checks: Built-in health and info endpoints
Installation
# Core package
uv pip install servekat
# With Sentry support
uv pip install "servekat[sentry]"
# Everything
uv pip install "servekat[all]"
Quick Start
Basic Server
from fastapi import FastAPI
from servekat.server.discovery import mount_discovered_routes, mount_routers
app = FastAPI()
# Mount library routers (health, info, etc.)
mount_routers(app, [
"servekat.server.api.health:router",
"servekat.server.api.info:router",
])
# Auto-discover and mount your app routers
mount_discovered_routes(app, "myapp.server.routers")
Using Configuration
from servekat.config import config
from servekat.server.server import serve_from_config
conf = config("config.yaml")
app = serve_from_config(conf)
Example config.yaml:
name: myapp
server:
host: 0.0.0.0
port: 8080
# Manual router imports
routers:
- "myapp.server.routers.users:router"
- "myapp.server.routers.posts:router"
# Auto-discovery (runs after manual routers)
router_discovery:
enabled: true
modules:
- "myapp.server.routers"
- "myapp.admin.routers"
pattern: "*_route.py"
exclude:
- "_internal_route.py"
Both manual routers and router_discovery work together - manual routers are mounted first, then auto-discovered routers are added.
Route Discovery
ServeKat can discover routers from:
- File patterns in your app packages
- Import strings from libraries
- Multiple modules at once
Pattern 1: Discover from Files
from servekat.server.discovery import mount_discovered_routes
# Discover all *_route.py files in myapp.server.routers
mount_discovered_routes(app, "myapp.server.routers")
# Custom pattern
mount_discovered_routes(app, "myapp.api", pattern="*_api.py")
# Multiple packages
mount_discovered_routes(app, [
"myapp.server.routers",
"myapp.admin.routers"
])
Pattern 2: Import from Libraries
from servekat.server.discovery import mount_routers
# Import routers directly from ServeKat or other libraries
mount_routers(app, [
"servekat.server.api.health:router",
"servekat.server.api.info:router",
"servekat.server.api.debug:router",
])
Pattern 3: Combined Approach
from servekat.server.discovery import mount_discovered_routes, mount_routers
# 1. Mount library routers
mount_routers(app, [
"servekat.server.api.health:router",
"servekat.server.api.info:router",
])
# 2. Discover and mount app routers
mount_discovered_routes(app, "myapp.server.routers")
API Schemas
Use ServeKat's base schemas for consistent API contracts:
from servekat.schemas import ResponseSchema, CreateSchema, UpdateSchema
from uuid import UUID
from datetime import datetime
class UserResponse(ResponseSchema):
id: UUID
name: str
email: str
created_at: datetime
class UserCreate(CreateSchema):
name: str
email: str
class UserUpdate(UpdateSchema):
name: str | None = None
email: str | None = None
See Schema Guide for complete documentation.
Exception Handling
ServeKat automatically maps CoreKat exceptions to HTTP responses:
from fastapi import APIRouter
from corekat.exceptions import NotFoundError
router = APIRouter()
@router.get("/users/{user_id}")
async def get_user(user_id: str):
user = await find_user(user_id)
if not user:
raise NotFoundError("User not found", {"user_id": user_id})
return user # Automatically becomes 404 response
See Error Handling Guide for details.
Development
- Run tests:
make test - Linting:
make lint - Type checking:
make pyright - All checks:
make check
License
BSD 3-Clause License - See LICENSE for details.
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
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 servekat-0.1.3.tar.gz.
File metadata
- Download URL: servekat-0.1.3.tar.gz
- Upload date:
- Size: 20.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5328d752a713b8a15ad088de4206106e15fa5cac881c6097f70927b0647352f
|
|
| MD5 |
11ff6ce35aa4f4f15d8c45cd44196d4a
|
|
| BLAKE2b-256 |
5eb06c31f8d83dd737c187ff7c4c31a96158d6f57be7d49eddb6e321ba1fc2df
|
File details
Details for the file servekat-0.1.3-py3-none-any.whl.
File metadata
- Download URL: servekat-0.1.3-py3-none-any.whl
- Upload date:
- Size: 30.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca9f835ac26bbfcd11d8bf51b7f3de7e79037aad5b1dcbfcc622e698e0893e70
|
|
| MD5 |
7f05d976ea22c0b8b6ea9bb8a5fcd7de
|
|
| BLAKE2b-256 |
fbe9db5050d30abe1a16a64165d7f408cfcdc165b6e5f888e38aa6694c7753e7
|