Asynchronous FastAPI toolkit with service bootstrap, resource orchestration, and observability utilities
Project description
Linglong Web
Linglong Web is a FastAPI toolkit for microservice scenarios, focusing on "simple startup, flexible extension, and complete observability".
中文文档:see README.zh-CN.md
Links
- GitHub: https://github.com/10000ms/linglong_web
- Issues: https://github.com/10000ms/linglong_web/issues
- Changelog: https://github.com/10000ms/linglong_web/blob/master/CHANGELOG.md
Why this library
When building microservices with FastAPI, you often need:
- Service lifecycle management - startup, shutdown, health checks, graceful termination
- Resource orchestration - database connections, cache, message queues, schedulers
- Observability - request-id propagation, structured logging, rate limiting
- Extensibility - plugin hooks for service registration, remote config, etc.
Linglong Web provides all these out of the box as modular components.
Features
- AppServer lifecycle - Built-in middleware, error handling, health checks, and signal handling
- Extension hooks -
on_config_ready,on_app_created,on_startup,on_shutdownfor custom logic injection - Thread-safe config -
LinglongConfigwith hot reload support - Resource management - Unified initialization for PostgreSQL, Redis, MongoDB, RabbitMQ, Celery
- HTTP client - aiohttp wrapper with request-id propagation and unified error handling
- Decorators - Redis cache, rate limit, and distributed lock decorators
- Scheduler - aioclock integration for periodic tasks
- Bilingual comments - Chinese and English comments throughout the codebase
Installation
# Core: FastAPI bootstrapper + HTTP client + scheduler + Redis-backed decorators
pip install linglong-web
# Add resource backends on demand
pip install "linglong-web[postgres]" # PostgreSQL (asyncpg + SQLAlchemy)
pip install "linglong-web[mongo]" # MongoDB (PyMongo async)
pip install "linglong-web[rabbitmq]" # RabbitMQ (aio-pika)
pip install "linglong-web[celery]" # Celery
pip install "linglong-web[all]" # everything
import linglong_web always works with just the core install; a backend you
haven't installed only raises a clear error if you actually initialize it.
Quick Start
import asyncio
from linglong_web import LinglongConfigBase, init_config
from linglong_web import build_success_response
from linglong_web import BaseRoute, ServerRouter
from linglong_web import LinglongAppServer
class DevConfig(LinglongConfigBase):
"""Development configuration"""
DEBUG = True
SERVICE_NAME = "demo-service"
router = ServerRouter()
router.initialize([
BaseRoute(
path="/ping",
method="GET",
handler=lambda: build_success_response({"pong": True}),
)
])
init_config({"dev": DevConfig}, mode_name="dev")
async def main():
app_server = LinglongAppServer()
await app_server.initialize(
service_name="demo-service",
router=router.get_router(),
config_dict={"dev": DevConfig},
)
await app_server.start(host="0.0.0.0", port=8080)
if __name__ == "__main__":
asyncio.run(main())
Module Overview
| Module | Description |
|---|---|
server |
FastAPI bootstrapper with middlewares & extension hooks |
server_extensions |
Lifecycle extension protocol for custom capabilities |
config |
Thread-safe config proxy with hot reload |
resource |
Unified resource bootstrapper (DB, Cache, MQ, Scheduler) |
http |
aiohttp client with request-id injection |
cacher |
Redis cache decorator |
limiter |
Rate limit decorator |
cluster_lock |
Distributed lock decorator |
scheduler |
aioclock helpers for periodic tasks |
utils |
Context, logging, signal handling, time utilities |
Extension Example
from linglong_web.server_extensions import BaseServerExtension
class RegistryExtension(BaseServerExtension):
"""Example: custom service registration logic"""
def __init__(self, client):
self._client = client
async def on_startup(self, server):
payload = {"service": server.service_name, "instance": server.instance_id}
await self._client.register(payload)
server.register_shutdown_callback(lambda: self._client.deregister(payload))
extensions = [RegistryExtension(client)]
await app_server.initialize(
service_name="demo",
router=router.get_router(),
config_dict={"dev": DevConfig},
extensions=extensions,
)
Linglong no longer bundles service registration or remote config capabilities. Implement them in your business repository using extension hooks.
Testing & Coverage
# Run tests with coverage
pytest tests/ -v --cov=linglong_web --cov-report=term-missing
# Or with PYTHONPATH
PYTHONPATH=. pytest tests/ -v --cov=linglong_web --cov-report=term-missing
- All new modules should maintain 80%+ test coverage.
- Use
pytest-asyncioandpytest-mockfor fine-grained mocking of resources, rate limiters, and signal handlers.
Project Layout
linglong_web/
├── linglong_web/
│ ├── core/ # Core modules
│ │ ├── server.py # FastAPI bootstrapper
│ │ ├── config.py # Config proxy
│ │ ├── resource.py # Resource manager
│ │ ├── http.py # HTTP client
│ │ ├── cacher.py # Cache decorator
│ │ ├── limiter.py # Rate limit decorator
│ │ └── ...
│ └── utils/ # Utilities
├── tests/ # Test suite
└── docs/ # Documentation
Versioning
The package version is sourced from linglong_web/__version__.py.
License
MIT License. See LICENSE.
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 linglong_web-0.0.1.tar.gz.
File metadata
- Download URL: linglong_web-0.0.1.tar.gz
- Upload date:
- Size: 78.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5cebae4d4ad93991af0acee95dfde00a3111a5ac28f2fbbc6169d5e1849ef22f
|
|
| MD5 |
6c3b9b0f187616c4909dc918859a1b0a
|
|
| BLAKE2b-256 |
42c6b3ca20a21629bb83735ab236c2e62d2603c496625e7d31313b4d5499f6a0
|
Provenance
The following attestation bundles were made for linglong_web-0.0.1.tar.gz:
Publisher:
publish.yml on 10000ms/linglong_web
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
linglong_web-0.0.1.tar.gz -
Subject digest:
5cebae4d4ad93991af0acee95dfde00a3111a5ac28f2fbbc6169d5e1849ef22f - Sigstore transparency entry: 1809469188
- Sigstore integration time:
-
Permalink:
10000ms/linglong_web@711dff4c375836ac87b2fe74535589fa9f9dc3b0 -
Branch / Tag:
refs/tags/0.0.1 - Owner: https://github.com/10000ms
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@711dff4c375836ac87b2fe74535589fa9f9dc3b0 -
Trigger Event:
release
-
Statement type:
File details
Details for the file linglong_web-0.0.1-py3-none-any.whl.
File metadata
- Download URL: linglong_web-0.0.1-py3-none-any.whl
- Upload date:
- Size: 64.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
307833814f0593dabc2a4163aa55de93e8e6fde4942ce2f2e98f8610a817f204
|
|
| MD5 |
7d1341e0f5768f1d943c1a6cdcca3304
|
|
| BLAKE2b-256 |
3db46b96e21436b09c18da519379fc33a520d2c422ccf453999de8fb4c75a827
|
Provenance
The following attestation bundles were made for linglong_web-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on 10000ms/linglong_web
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
linglong_web-0.0.1-py3-none-any.whl -
Subject digest:
307833814f0593dabc2a4163aa55de93e8e6fde4942ce2f2e98f8610a817f204 - Sigstore transparency entry: 1809469191
- Sigstore integration time:
-
Permalink:
10000ms/linglong_web@711dff4c375836ac87b2fe74535589fa9f9dc3b0 -
Branch / Tag:
refs/tags/0.0.1 - Owner: https://github.com/10000ms
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@711dff4c375836ac87b2fe74535589fa9f9dc3b0 -
Trigger Event:
release
-
Statement type: