LilyWhite Framework
春が来た。
基于 Litestar + Uvicorn 的 Python 服务端框架。 你理解为 Python 版 Spring 就行!
装饰器一贴,构造器一写,剩下的交给容器。分层、DI、生命周期、多存储后端,都已经替你备好了。
没有代理、AOP 和字节码魔法,容器本身就是普通 Python 代码,想看随时能看懂。
Lily White 是报春妖精。这个框架也想做点类似的事:把 Spring 的开发体验带进 Python,再涂上一点幻想乡的颜色。
先来跑一下
准备 Python 3.14+ 和 uv:
uv sync
cp -n config.example.yaml config.yaml
uv run python -m example
PowerShell 复制配置:
Copy-Item config.example.yaml config.yaml
uv run python -m example
然后打开:
问候接口 http://127.0.0.1:8000/api/v1/greetings/幻想乡
健康检查 http://127.0.0.1:8000/healthz
请求指标 http://127.0.0.1:8000/metrics
启动时会打印彩色 Banner、当前地址和实际启用的后端。
写个入口类
# example/application.py
from lilywhite import LilywhiteMain
@LilywhiteMain
class ExampleApplication:
application_name = "LilyWhite Greeting Example"
application_version = "0.2.0"
backends = ("memory",)
async def on_startup(self) -> None:
...
async def on_shutdown(self) -> None:
...
application_name 和 application_version 会出现在 Banner 上,backends 是默认存储后端。
想加点自己的启动逻辑,就写 on_startup();需要收拾资源,就写 on_shutdown()。同步、异步都可以,框架会把它们接进生命周期。
再用 run() 启动:
# example/__main__.py
from lilywhite import run
from .application import ExampleApplication
if __name__ == "__main__":
run(ExampleApplication)
入口类在哪个顶层包,run() 就扫描哪个包里的 controller、services 和 storage。
换个 ASGI 服务器也行
@LilywhiteMain 会给入口类注入 ASGI __call__(),所以入口实例本身就是应用:
import uvicorn
from example.application import ExampleApplication
uvicorn.run(ExampleApplication(), host="127.0.0.1", port=8000)
Litestar 应用会在首次调用时创建并缓存,HTTP、WebSocket 和 lifespan 都走同一个实例。外部服务器自己指定监听地址时,Banner 会把地址管理交给它。
业务怎么写
装饰器都可以从顶层拿到:
from lilywhite import component, controller, repository, service, websocket_controller
Controller
@controller("/api/v1")
class GreetingController(Controller):
@get("/greetings/{name:str}")
async def greeting(self, name: str) -> Response[HttpResult]:
...
根路径直接写在装饰器里。裸写 @controller,默认就是 /。
Controller 管 HTTP 参数和结果映射,Service 通过 NamedDependency[具体Service] 注入。完整例子在 example/controller/greeting.py。
WebSocket Controller
from litestar.handlers.websocket_handlers import WebsocketListener
from lilywhite import websocket_controller
@websocket_controller("/ws/echo")
class EchoWebsocketController(WebsocketListener):
async def on_receive(self, data: str) -> str:
return f"LilyWhite 回声:{data}"
它和 @controller 一样声明路径并随业务包自动发现,只不过处理的是 WebSocket 消息。可以连接 ws://127.0.0.1:8000/ws/echo,发一句“春天来了”试试。
Service
@service
class GreetingService:
def __init__(self, greeting_repository: GreetingRepository) -> None:
self._greeting_repository = greeting_repository
需要什么依赖,就写进构造器。Service 里不用关心它来自内存、MySQL 还是别的存储。
Repository 与 Adapter
接口放在业务包的 storage/,实现放进对应后端子包:
@repository(GreetingRepository, backend="memory")
class MemoryGreetingRepository:
...
在 example/storage/__init__.py 里声明有哪些后端:
backend_packages = ("memory",)
启动时选一个,或者多选:
uv run python -m example --backend memory
uv run python -m example --backend mysql --backend redis
容器会按 Repository Interface 找出实现。多个后端同时提供同一个接口时,启动会直接告诉你发生了冲突。
托管组件
需要随应用启动和关闭的组件,实现 ManagedComponent:
@component
class MyComponent:
async def initialize(self) -> None: ...
async def close(self) -> None: ...
Singleton 组件会正序启动、逆序关闭,并出现在 /healthz 的组件状态里。
目录长这样
lilywhite/ 框架本体
app/ 入口、ASGI、DI、发现和生命周期
controller/ Controller、统一结果、SSE 和 HTTP 中间件
services/ 通用 Service、结果模型和 @service
storage/ @repository 和后端解析
domain/ 通用类型
observability/ 日志历史和异步订阅
utils/ @component、注册表、任务、时钟和随机源
config.py 配置模型和 YAML 加载
logger.py Loguru 配置
health.py 组件健康状态
metrics.py 请求指标
banner.py 东方主题彩色 Banner
example/ 问候示例
application.py @LilywhiteMain 入口类
__main__.py 命令行启动
controller/ 问候 Controller
services/ 问候 Service
storage/ Repository 接口与内存 Adapter
配置
config.example.yaml 是公共示例,config.yaml 留给你自己的环境,不会进 Git。
server:监听地址、端口、CORS。logging:日志级别、控制台、文件轮转与压缩。operations:指标窗口和实时日志 Token。
配置分组直接写 section(),不用为每个分组再写一个类。字段的范围、长度和正则约束交给 msgspec.Meta:
from msgspec import Meta
from lilywhite.config import Config, section, setting
class AppConfig(Config):
server = section(
host=setting("127.0.0.1", meta=Meta(min_length=1)),
port=setting(8000, meta=Meta(ge=0, le=65535)),
)
需要跨字段条件时,再在类上叠加 @validate("字段路径", lambda ...)。
读取时仍然是 config.server.port。需要覆盖时调用 override(),会得到一份独立副本:
config = AppConfig().override(server={"port": 9527})
开发
uv sync --locked
uv run pytest tests -q
uv run ruff format --check lilywhite example tests
uv run ruff check lilywhite example tests
uv run mypy
uv build
uv build 会生成源码包和 wheel,发行包里只有 lilywhite 框架本体。
Metadata
Release files for lilywhite 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| lilywhite-0.2.0.tar.gz | 39.7 kB | With provenance | Details for lilywhite-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| lilywhite-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for lilywhite-0.2.0-py3-none-any.whl |
Total release size: 94.7 kB
Release files / lilywhite-0.2.0.tar.gz
| Download URL | lilywhite-0.2.0.tar.gz |
|---|---|
| Size | 39.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cab8bff1859b180932398655deea15175665a71662a37d091b797eff5a81bfcd
|
|
BLAKE2b-256 checksum How to use checksums |
a85aa13ba88b086f5969e2f921d8401247a9a1dbf345faccc8d21eddfeca7a2d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.
Transparency logRelease files / lilywhite-0.2.0-py3-none-any.whl
| Download URL | lilywhite-0.2.0-py3-none-any.whl |
|---|---|
| Size | 55.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
810c1d4a5afdb3ac2727bdbe25639b69442a6ef71bcd9a4cf68b24db0f11803a
|
|
BLAKE2b-256 checksum How to use checksums |
987795d344f3d43d5a1992a1bbf06a64d7a9110a64e3793304240ed2993d06ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.
Transparency logSecurity for lilywhite 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for lilywhite-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.