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.3.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.3-py3-none-any.whl (30.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lumary-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 d472eba0cec7714555c7cc102abdbb1ceb748778f5b88bb8e7ff8cf8f6b884ce
MD5 731bfc1a198813335ecacce2a7aa97e7
BLAKE2b-256 0d345b52411250b6d440c5e9b227bbeb12159a53ef2e6f0be35a494fc92bf5ab

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: lumary-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7adb61b0f2fed9a6731151c016b353614c856b42fa8fe60d4024e1243ebc688f
MD5 f85ca064b746366f613653d259fd794a
BLAKE2b-256 4476133dba1b8c3dff3781be80ac8a6788234bb3eaec5ce953f03de6bf9e8594

See more details on using hashes here.

Provenance

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