Skip to main content

Infrastructure for building and deploying prod-ready services

Project description

svc-infra

License: MIT Python 3.11+ FastAPI

Infrastructure library for building and deploying production-ready services with FastAPI, SQLAlchemy, authentication, observability, and more.

Features

Core Infrastructure

  • FastAPI Integration: Pre-configured FastAPI application with middleware, error handling, and routing
  • Database Support: Multiple database backends (PostgreSQL, MySQL, SQLite, SQL Server, Snowflake, DuckDB)
  • Authentication: OAuth integration with FastAPI-Users
  • CLI Tools: Command-line interface for database migrations, scaffolding, and observability
  • Observability: OpenTelemetry tracing, Prometheus metrics, and Grafana dashboards
  • MCP Integration: Model Context Protocol support for AI integrations

Database Management

  • SQL: SQLAlchemy 2.0 with async support
  • Migrations: Alembic integration for database schema management
  • NoSQL: MongoDB support with Motor
  • Multi-database: Support for multiple database connections

Authentication & Authorization

  • OAuth providers integration
  • User management with FastAPI-Users
  • Secure password hashing with bcrypt
  • Email validation
  • JWT token handling

Observability

  • OpenTelemetry distributed tracing
  • Prometheus metrics collection
  • Grafana dashboard templates
  • Request/response logging
  • Error tracking and monitoring

Installation

Basic Installation

pip install svc-infra

With Database Extras

# PostgreSQL (v3)
pip install svc-infra[pg]

# PostgreSQL (v2)
pip install svc-infra[pg2]

# SQLite
pip install svc-infra[sqlite]

# MySQL
pip install svc-infra[mysql]

# SQL Server
pip install svc-infra[mssql]

# Snowflake
pip install svc-infra[snowflake]

# DuckDB
pip install svc-infra[duckdb]

# All databases
pip install svc-infra[pg,sqlite,mysql,mssql,snowflake,duckdb]

With Metrics Support

pip install svc-infra[metrics]

Quick Start

1. Create a FastAPI Application

from fastapi import FastAPI
from svc_infra.api.fastapi import create_app
from svc_infra.app.settings import AppSettings

# Create your app settings
settings = AppSettings()

# Create the FastAPI app with svc-infra
app = create_app(settings)

@app.get("/")
async def root():
    return {"message": "Hello from svc-infra!"}

2. Database Setup

from svc_infra.db.sql import setup_database

# Setup database connection
await setup_database(settings.database_url)

3. Add Authentication

from svc_infra.auth import add_auth

# Add authentication to your FastAPI app
add_auth(app, settings)

4. CLI Usage

The library provides a CLI for common tasks:

# Database migrations
svc-infra alembic init
svc-infra alembic migrate "Initial migration"
svc-infra alembic upgrade

# Generate database scaffolding
svc-infra sql scaffold --table users --model User

# Observability setup
svc-infra obs setup

Configuration

Environment Variables

Create a .env file or set environment variables:

# Database
DATABASE_URL=postgresql+asyncpg://user:pass@localhost/db

# Authentication
SECRET_KEY=your-secret-key
OAUTH_CLIENT_ID=your-oauth-client-id
OAUTH_CLIENT_SECRET=your-oauth-client-secret

# Observability
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
PROMETHEUS_PORT=8000

Settings Class

from svc_infra.app.settings import AppSettings

settings = AppSettings(
    database_url="postgresql+asyncpg://user:pass@localhost/db",
    secret_key="your-secret-key",
    debug=True
)

Project Structure

svc-infra/
├── src/svc_infra/
│   ├── api/           # FastAPI integration and middleware
│   ├── app/           # Application settings and configuration
│   ├── auth/          # Authentication and authorization
│   ├── cli/           # Command-line interface
│   ├── db/            # Database connections and utilities
│   ├── mcp/           # Model Context Protocol integration
│   └── observability/ # Monitoring, metrics, and tracing
├── tests/             # Test suite
└── pyproject.toml     # Project configuration

Database Support

Supported Databases

Database Driver Extra Connection String Example
PostgreSQL (v3) asyncpg/psycopg [pg] postgresql+asyncpg://user:pass@host/db
PostgreSQL (v2) psycopg2 [pg2] postgresql://user:pass@host/db
SQLite aiosqlite [sqlite] sqlite+aiosqlite:///path/to/db.sqlite
MySQL pymysql [mysql] mysql+pymysql://user:pass@host/db
SQL Server pyodbc [mssql] mssql+pyodbc://user:pass@host/db
Snowflake snowflake [snowflake] snowflake://user:pass@account/db
DuckDB duckdb [duckdb] duckdb:///path/to/db.duckdb

Authentication

OAuth Providers

The library supports multiple OAuth providers through FastAPI-Users:

  • Google
  • GitHub
  • Microsoft
  • Discord
  • And more...

Example OAuth Setup

from svc_infra.auth.providers import setup_oauth_providers

oauth_providers = setup_oauth_providers({
    "google": {
        "client_id": "your-google-client-id",
        "client_secret": "your-google-client-secret"
    }
})

Observability

Tracing

OpenTelemetry tracing is automatically configured for:

  • FastAPI requests
  • SQLAlchemy database operations
  • HTTP client requests (httpx, requests)

Metrics

Prometheus metrics are collected for:

  • Request duration and count
  • Database query performance
  • Custom business metrics

Dashboards

Pre-built Grafana dashboards are included for:

  • API performance monitoring
  • Database metrics
  • System health checks

CLI Commands

Command Description
svc-infra alembic init Initialize Alembic migrations
svc-infra alembic migrate <message> Create new migration
svc-infra alembic upgrade Apply migrations
svc-infra sql scaffold Generate database models and schemas
svc-infra obs setup Setup observability stack

Development

Prerequisites

  • Python 3.11+
  • Poetry (for dependency management)

Setup Development Environment

# Clone the repository
git clone https://github.com/your-org/svc-infra.git
cd svc-infra

# Install dependencies
poetry install --all-extras

# Install pre-commit hooks
pre-commit install

# Run tests
poetry run pytest

Running Tests

# Run all tests
poetry run pytest

# Run with coverage
poetry run pytest --cov=svc_infra

# Run specific test file
poetry run pytest tests/auth/test_providers.py

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for your changes
  5. Run the test suite (poetry run pytest)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

See CHANGELOG.md for a list of changes in each version.

Support


Built️ for production-ready Python services

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

svc_infra-0.1.220.tar.gz (73.6 kB view details)

Uploaded Source

Built Distribution

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

svc_infra-0.1.220-py3-none-any.whl (109.8 kB view details)

Uploaded Python 3

File details

Details for the file svc_infra-0.1.220.tar.gz.

File metadata

  • Download URL: svc_infra-0.1.220.tar.gz
  • Upload date:
  • Size: 73.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for svc_infra-0.1.220.tar.gz
Algorithm Hash digest
SHA256 08d63e4f59f8978b8504c981cbebeeef5757c93d7947dc6e561e57e5768cffd5
MD5 a2e44927e227a1c2d07e39d8eb6bad71
BLAKE2b-256 cde22bdefd17d210314f0c27bac63f214f749997d2c56720907c81facf66f2ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for svc_infra-0.1.220.tar.gz:

Publisher: publish-pypi.yml on Aliikhatami94/svc-infra

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

File details

Details for the file svc_infra-0.1.220-py3-none-any.whl.

File metadata

  • Download URL: svc_infra-0.1.220-py3-none-any.whl
  • Upload date:
  • Size: 109.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for svc_infra-0.1.220-py3-none-any.whl
Algorithm Hash digest
SHA256 959fca92d1a2f810eb156455f3e59c1aca0e3fd58189bc8da4c638cf78d9e105
MD5 6cd832a7b34ce6e52f772bacc7134b69
BLAKE2b-256 6a8597b7bbc532bfa5ddfbe7d37de96bfc9335e283c36f2e53ff3ceaea271a8c

See more details on using hashes here.

Provenance

The following attestation bundles were made for svc_infra-0.1.220-py3-none-any.whl:

Publisher: publish-pypi.yml on Aliikhatami94/svc-infra

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