Skip to main content

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:

  1. File patterns in your app packages
  2. Import strings from libraries
  3. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

servekat-0.1.2.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

servekat-0.1.2-py3-none-any.whl (30.7 kB view details)

Uploaded Python 3

File details

Details for the file servekat-0.1.2.tar.gz.

File metadata

  • Download URL: servekat-0.1.2.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for servekat-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ec1b80c6c43c7d0a0716348badda66e9bfed9e0087f37c9ed3461e344c11ecaf
MD5 b9b7972c127e5402ff4b1aca0d70cbe1
BLAKE2b-256 30053bfdcb2c327731c1eeac15f91c6063d90a2abf7bf9ed258cf6441228c17d

See more details on using hashes here.

File details

Details for the file servekat-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: servekat-0.1.2-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

Hashes for servekat-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1be1405b78f3bb84cd694294afe82ad34d533665d66f9a838049477bfc077d11
MD5 30c32a0bd92724eaa35bde5b605e9531
BLAKE2b-256 278c34fd5155fe0b8293dd52fa57cbdacef984df4cefda944e276966454e65b6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page