Skip to main content

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.1.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

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。

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。

配置分区是不可变的 msgspec.Struct。新增分区后,容器可以按具体 Properties 类型注入。

开发

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.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lilywhite 0.1.0
File Size Uploaded Provenance Actions
lilywhite-0.1.0.tar.gz 37.0 kB With provenance Details for lilywhite-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lilywhite 0.1.0
File Interpreter ABI Platform Actions
lilywhite-0.1.0-py3-none-any.whl Python 3 none any Details for lilywhite-0.1.0-py3-none-any.whl

Total release size: 88.7 kB

Release files / lilywhite-0.1.0.tar.gz

Download URL lilywhite-0.1.0.tar.gz
Size 37.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2571de9974f6edfd1ff1043b9c166fea0507f44b65cbb0475afc8a3e85d978c9
BLAKE2b-256 checksum
How to use checksums
681bba3d6cf5630347280c8c85c1f9614c30d8ac8ff48f1247a885c80c21e8cd
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 log

Release files / lilywhite-0.1.0-py3-none-any.whl

Download URL lilywhite-0.1.0-py3-none-any.whl
Size 51.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f183eca3b2aed8f17fca4feba1875c09ae7c30ddb5911a828a585837c10867f
BLAKE2b-256 checksum
How to use checksums
4e498f5c9ad65bf47fbd9eadac7a45e9e9285f27d4810f1a3ebe0c1e8578beb6
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 log

Security for lilywhite 0.1.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.

Select from 2 files

Attestation for lilywhite-0.1.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 log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page