Skip to main content

基于 FastAPI 封装的生产级 Web 应用框架

Project description

Lumary

基于 FastAPI 封装的生产级 Web 应用框架,开箱即用。

特性

  • 应用封装Lumary 类继承 FastAPI,内置异常处理、CORS 中间件、自定义 OpenAPI 文档、健康检查
  • 生命周期钩子@on_startup / @on_shutdown 装饰器,支持优先级排序与异常终止控制
  • 统一响应格式APIResponsePageDataPageQuery 等标准化 Schema
  • 业务异常BusinessException 支持自定义错误码与消息,全局自动捕获
  • WebSocket 管理WSConnectionManager 支持分组、单播、广播、上下文管理器
  • 枚举基类BaseEnum 提供 val / label 属性访问
  • SQLAlchemy 混入SoftDeleteMixin 软删除支持(可选依赖)
  • 子应用管控 — 支持动态挂载子应用,禁止子应用嵌套

安装

# 最小安装(仅核心,无数据库依赖)
pip install lumary

# 仅 SQLAlchemy 支持
pip install lumary[sqlalchemy]

# 标准安装(含 SQLAlchemy + Uvicorn + pydantic-settings)
pip install lumary[standard]

快速开始

from lumary import Lumary, on_startup, on_shutdown

app = Lumary(title='My Project', debug=True, enable_health_check=True)


@on_startup(priority=100)
async def connect_db():
    print('Database connected')


@on_shutdown
async def close_db():
    print('Database closed')

WebSocket

from fastapi import WebSocket
from lumary import Lumary, WSConnectionManager

app = Lumary(title='Chat')
manager = WSConnectionManager()


@app.websocket('/ws/{room}')
async def ws_endpoint(websocket: WebSocket, room: str):
    async with manager.lifespan(websocket, group=room) as cid:
        while True:
            data = await websocket.receive_json()
            await manager.broadcast_json(data, group=room, exclude={cid})

统一响应

from lumary import response_success, response_fail, APIResponse

@app.get('/users/{uid}')
async def get_user(uid: int) -> APIResponse:
    user = await fetch_user(uid)
    if not user:
        return response_fail(code=404, message='用户不存在')
    return response_success(data=user)

业务异常

from lumary import BusinessException

async def transfer(from_id: int, to_id: int, amount: float):
    if amount <= 0:
        raise BusinessException(code=400, message='金额必须大于零')
    ...

模块一览

模块 说明
lumary.application 应用类 Lumary
lumary.lifespan 生命周期钩子 on_startup / on_shutdown
lumary.schemas 统一响应模型 APIResponse / PageData / PageQuery
lumary.exceptions 业务异常 BusinessException
lumary.handlers 全局异常处理器
lumary.middleware 中间件注册
lumary.openapi 自定义 OpenAPI 文档
lumary.websocket WebSocket 连接管理器 WSConnectionManager
lumary.common.enums 枚举基类 BaseEnum
lumary.common.mixins SQLAlchemy 混入类
lumary.db 数据库工具

开发

# 克隆仓库
git clone https://github.com/zarkhan/lumary.git
cd lumary

# 创建虚拟环境 & 安装开发依赖
python -m venv .venv
.venv\Scripts\activate       # Windows
source .venv/bin/activate    # macOS / Linux
pip install -e ".[dev]"

# 代码检查
ruff check lumary/

# 运行测试
pytest

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lumary-0.1.2.tar.gz (25.3 kB view details)

Uploaded Source

Built Distribution

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

lumary-0.1.2-py3-none-any.whl (30.7 kB view details)

Uploaded Python 3

File details

Details for the file lumary-0.1.2.tar.gz.

File metadata

  • Download URL: lumary-0.1.2.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for lumary-0.1.2.tar.gz
Algorithm Hash digest
SHA256 007b7c4bfb392167a0c2d3ad7d2a3087748e195f9e38968a704ce3eaa0a587d8
MD5 10e2bca8dc52a8c5ee1ff8d831ee6781
BLAKE2b-256 39e6762d10ff2f88715b97406849f0b70332a894c3bee0b64df956dac4fea866

See more details on using hashes here.

Provenance

The following attestation bundles were made for lumary-0.1.2.tar.gz:

Publisher: publish.yml on hgz1989/pkg_lumary

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

File details

Details for the file lumary-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: lumary-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 30.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for lumary-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8ef3123095f76d80c060525ccc2fef909d98d221297cd7eef228279b3210f81f
MD5 dd0c6485858f744fdf6c3066aa77828a
BLAKE2b-256 cc4fe60e9f8d5051b76ec2d1cbbe1ac0742d01b030ba0f1c100cd39e3486ba0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for lumary-0.1.2-py3-none-any.whl:

Publisher: publish.yml on hgz1989/pkg_lumary

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