Infrastructure for building and deploying prod-ready services
Project description
svc-infra
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:
- 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
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for your changes
- Run the test suite (
poetry run pytest) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - 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
- Email: aliikhatami94@gmail.com
- Issues: GitHub Issues
- Documentation: GitHub README
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
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08d63e4f59f8978b8504c981cbebeeef5757c93d7947dc6e561e57e5768cffd5
|
|
| MD5 |
a2e44927e227a1c2d07e39d8eb6bad71
|
|
| BLAKE2b-256 |
cde22bdefd17d210314f0c27bac63f214f749997d2c56720907c81facf66f2ca
|
Provenance
The following attestation bundles were made for svc_infra-0.1.220.tar.gz:
Publisher:
publish-pypi.yml on Aliikhatami94/svc-infra
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
svc_infra-0.1.220.tar.gz -
Subject digest:
08d63e4f59f8978b8504c981cbebeeef5757c93d7947dc6e561e57e5768cffd5 - Sigstore transparency entry: 496641499
- Sigstore integration time:
-
Permalink:
Aliikhatami94/svc-infra@71f1c7537e19fbec08626efcb642a928e5b94a6f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Aliikhatami94
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@71f1c7537e19fbec08626efcb642a928e5b94a6f -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
959fca92d1a2f810eb156455f3e59c1aca0e3fd58189bc8da4c638cf78d9e105
|
|
| MD5 |
6cd832a7b34ce6e52f772bacc7134b69
|
|
| BLAKE2b-256 |
6a8597b7bbc532bfa5ddfbe7d37de96bfc9335e283c36f2e53ff3ceaea271a8c
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
svc_infra-0.1.220-py3-none-any.whl -
Subject digest:
959fca92d1a2f810eb156455f3e59c1aca0e3fd58189bc8da4c638cf78d9e105 - Sigstore transparency entry: 496641507
- Sigstore integration time:
-
Permalink:
Aliikhatami94/svc-infra@71f1c7537e19fbec08626efcb642a928e5b94a6f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Aliikhatami94
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@71f1c7537e19fbec08626efcb642a928e5b94a6f -
Trigger Event:
push
-
Statement type: