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[asyncio] 支持
pip install lumary[sqlalchemy]

# 标准安装(含 fastapi-cli + SQLAlchemy[asyncio] + 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 数据库工具

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.4.tar.gz (24.7 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.4-py3-none-any.whl (30.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lumary-0.1.4.tar.gz
  • Upload date:
  • Size: 24.7 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.4.tar.gz
Algorithm Hash digest
SHA256 53e876c8619589b6030da91206f386f8be39285629624dfdfe57c56ecccbeee9
MD5 02f2f859f895480faab1e64c2a5db0be
BLAKE2b-256 785832e8db2e072a4793a288fd9ba01fa94902a66ef3c1a5571152656df32e92

See more details on using hashes here.

Provenance

The following attestation bundles were made for lumary-0.1.4.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.4-py3-none-any.whl.

File metadata

  • Download URL: lumary-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 30.4 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8031284cd1873b8364615b858ec4efeb8f0934599ee74bc2f5753431e07792d6
MD5 874ca80aee13b69e87688df9369d071d
BLAKE2b-256 8d9e0158739906f2002b263cfd5db4e399355afa6bcce2d9d217872b5a3247c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for lumary-0.1.4-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