guzhi/app/__init__.py
邹方成 cc352d3184 feat: 重构后端服务并添加新功能
refactor: 优化API路由和响应模型
feat(admin): 添加App用户管理接口
feat(sms): 实现阿里云短信服务集成
feat(email): 添加SMTP邮件发送功能
feat(upload): 支持文件上传接口
feat(rate-limiter): 实现手机号限流器
fix: 修复计算步骤入库问题
docs: 更新API文档和测试计划
chore: 更新依赖和配置
2025-11-19 19:36:03 +08:00

68 lines
3.2 KiB
Python

from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from tortoise import Tortoise
from app.core.exceptions import SettingNotFound
from app.core.init_app import (
init_data,
make_middlewares,
register_exceptions,
register_routers,
)
try:
from app.settings.config import settings
except ImportError:
raise SettingNotFound("Can not import settings")
@asynccontextmanager
async def lifespan(app: FastAPI):
await init_data()
yield
await Tortoise.close_connections()
def create_app() -> FastAPI:
openapi_tags = [
{"name": "app-用户认证与账户", "description": "用户端账户与认证相关接口(公开/需认证以端点说明为准)"},
{"name": "app-估值评估", "description": "用户端估值评估相关接口(需用户端认证)"},
{"name": "app-短信服务", "description": "用户端短信验证码与登录相关接口(公开)"},
{"name": "app-上传", "description": "用户端文件上传接口(公开)"},
{"name": "admin-基础", "description": "后台登录与个人信息接口(部分公开,其他需认证)"},
{"name": "admin-用户管理", "description": "后台用户管理接口(需认证与权限)"},
{"name": "admin-角色管理", "description": "后台角色管理接口(需认证与权限)"},
{"name": "admin-菜单管理", "description": "后台菜单管理接口(需认证与权限)"},
{"name": "admin-API权限管理", "description": "后台 API 权限管理接口(需认证与权限)"},
{"name": "admin-部门管理", "description": "后台部门管理接口(需认证与权限)"},
{"name": "admin-审计日志", "description": "后台审计日志查询接口(需认证与权限)"},
{"name": "admin-估值评估", "description": "后台估值评估接口(需认证与权限)"},
{"name": "admin-发票管理", "description": "后台发票与抬头管理接口(需认证与权限)"},
{"name": "admin-交易管理", "description": "后台交易/对公转账记录接口(需认证与权限)"},
{"name": "admin-内置接口", "description": "后台第三方内置接口调用(需认证与权限)"},
{"name": "admin-行业管理", "description": "后台行业数据管理(当前公开)"},
{"name": "admin-指数管理", "description": "后台指数数据管理(当前公开)"},
{"name": "admin-政策管理", "description": "后台政策数据管理(当前公开)"},
{"name": "admin-ESG管理", "description": "后台 ESG 数据管理(当前公开)"},
]
app = FastAPI(
title=settings.APP_TITLE,
description=settings.APP_DESCRIPTION,
version=settings.VERSION,
openapi_url="/openapi.json",
openapi_tags=openapi_tags,
middleware=make_middlewares(),
lifespan=lifespan,
redirect_slashes=False, # 禁用尾部斜杠重定向
)
# 注册静态文件目录
# app.mount("/static", StaticFiles(directory="app/static"), name="static")
register_exceptions(app)
register_routers(app, prefix="/api")
return app
app = create_app()