Skip to main content

Async SQLAlchemy framework with FastAPI integration - reusable foundation for building data services

Project description

Servicekit

CI PyPI version codecov Python 3.13+ License: AGPL v3 Documentation

Async SQLAlchemy framework with FastAPI integration - reusable foundation for building data services

Servicekit is a framework-agnostic core library providing foundational infrastructure for building async Python services with FastAPI and SQLAlchemy.

Features

  • Database Layer: Async SQLAlchemy with SQLite support, connection pooling, and automatic migrations
  • Repository Pattern: Generic repository base classes for data access
  • Manager Pattern: Business logic layer with lifecycle hooks
  • CRUD API: Auto-generated REST endpoints with full CRUD operations
  • Authentication: API key middleware with file and environment variable support
  • Job Scheduling: Async job scheduler with concurrency control
  • App Hosting: Mount static web applications alongside your API
  • Monitoring: Prometheus metrics and OpenTelemetry integration
  • Health Checks: Flexible health check system with SSE streaming support
  • Error Handling: RFC 9457 Problem Details for HTTP APIs
  • Logging: Structured logging with request context

Installation

pip install servicekit

Quick Start

from servicekit.api import BaseServiceBuilder, ServiceInfo

app = (
    BaseServiceBuilder(info=ServiceInfo(id="my-service", display_name="My Service"))
    .with_health()
    .with_database("sqlite+aiosqlite:///./data.db")
    .build()
)

Architecture

servicekit/
├── database.py       # Database, SqliteDatabase, SqliteDatabaseBuilder
├── models.py         # Base, Entity ORM classes
├── repository.py     # Repository, BaseRepository
├── manager.py        # Manager, BaseManager
├── schemas.py        # EntityIn, EntityOut, PaginatedResponse
├── scheduler.py      # JobScheduler, AIOJobScheduler
├── exceptions.py     # Error classes
├── logging.py        # Structured logging
├── types.py          # ULIDType, JsonSafe
└── api/              # FastAPI framework layer
    ├── router.py     # Router base class
    ├── crud.py       # CrudRouter, CrudPermissions
    ├── auth.py       # API key authentication
    ├── app.py        # Static app hosting
    ├── middleware.py # Error handlers, logging
    └── routers/      # Health, Jobs, System, Metrics

Key Components

BaseServiceBuilder

from servicekit.api import BaseServiceBuilder, ServiceInfo

app = (
    BaseServiceBuilder(info=ServiceInfo(id="my-service", display_name="My Service"))
    .with_health()                    # Health check endpoint
    .with_database(url)               # Database configuration
    .with_jobs(max_concurrency=10)   # Job scheduler
    .with_auth()                      # API key authentication
    .with_monitoring()                # Prometheus metrics
    .with_app("./webapp")             # Static web app
    .include_router(custom_router)   # Custom routes
    .build()
)

Repository Pattern

from servicekit import BaseRepository, Entity
from sqlalchemy.orm import Mapped, mapped_column

class User(Entity):
    __tablename__ = "users"
    name: Mapped[str] = mapped_column()
    email: Mapped[str] = mapped_column()

class UserRepository(BaseRepository[User, ULID]):
    def __init__(self, session: AsyncSession):
        super().__init__(session, User)

CRUD Router

from servicekit.api import CrudRouter, CrudPermissions

router = CrudRouter.create(
    prefix="/api/v1/users",
    tags=["Users"],
    entity_in_type=UserIn,
    entity_out_type=UserOut,
    manager_factory=get_user_manager,
    permissions=CrudPermissions(create=True, read=True, update=True, delete=False)
)

Examples

See the examples/ directory for complete working examples:

  • core_api/main.py - Basic CRUD service
  • job_scheduler/main.py - Background job execution
  • app_hosting/main.py - Hosting static web apps
  • auth/main.py - API key authentication
  • monitoring/main.py - Prometheus metrics

Documentation

See docs/ for comprehensive guides and API reference.

Testing

make test      # Run tests
make lint      # Run linter
make coverage  # Test coverage

License

AGPL-3.0-or-later

Related Projects

  • chapkit - Domain modules (artifacts, configs, tasks, ML workflows) built on servicekit (docs)

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

servicekit-1.0.1.tar.gz (42.6 kB view details)

Uploaded Source

Built Distribution

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

servicekit-1.0.1-py3-none-any.whl (54.5 kB view details)

Uploaded Python 3

File details

Details for the file servicekit-1.0.1.tar.gz.

File metadata

  • Download URL: servicekit-1.0.1.tar.gz
  • Upload date:
  • Size: 42.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for servicekit-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a4cfdc5aaefa4c8ca3f03331b13f1036947b32312e62c5d760ade6dad37fb172
MD5 dce1ef303dbec502bb3a744c5043a694
BLAKE2b-256 b581d654464c575f4c45e3eb6a4e4ff45a846497a07064cef005556049f176b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for servicekit-1.0.1.tar.gz:

Publisher: release.yml on dhis2-chap/servicekit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file servicekit-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: servicekit-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 54.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for servicekit-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7845ac2845d4a2c26348f89c176ac495d367b657550c8b7b0a524954c2dfa145
MD5 212e09c74a5fbd449b1ec2feb958054b
BLAKE2b-256 5f1389c193de76a4ed8f802550de92ae16910073eea80b645f7e7b0e5b118dab

See more details on using hashes here.

Provenance

The following attestation bundles were made for servicekit-1.0.1-py3-none-any.whl:

Publisher: release.yml on dhis2-chap/servicekit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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