Initial commit: 智能项目定价模型

This commit is contained in:
kuzma
2026-01-31 21:33:06 +08:00
commit ef0824303f
174 changed files with 31705 additions and 0 deletions

127
后端服务/app/main.py Normal file
View File

@@ -0,0 +1,127 @@
"""FastAPI 应用入口
智能项目定价模型后端服务
遵循瑞小美系统技术栈标准
"""
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from app.config import settings
from app.database import init_db, close_db
from app.routers import health, categories, materials, equipments, staff_levels, fixed_costs, projects, market, pricing, profit, dashboard
from app.middleware import (
PerformanceMiddleware,
ResponseCacheMiddleware,
RateLimitMiddleware,
SecurityHeadersMiddleware,
)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""应用生命周期管理"""
# 启动时初始化数据库
await init_db()
yield
# 关闭时清理资源
await close_db()
# 创建 FastAPI 应用
app = FastAPI(
title=settings.APP_NAME,
version=settings.APP_VERSION,
description="智能项目定价模型 - 帮助机构精准核算成本、分析市场、智能定价",
docs_url="/docs" if settings.DEBUG else None,
redoc_url="/redoc" if settings.DEBUG else None,
lifespan=lifespan,
)
# 配置 CORS
app.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 性能优化中间件
app.add_middleware(GZipMiddleware, minimum_size=1000) # 压缩大于 1KB 的响应
app.add_middleware(PerformanceMiddleware) # 性能监控
app.add_middleware(ResponseCacheMiddleware) # 响应缓存
# 安全中间件
app.add_middleware(RateLimitMiddleware, enabled=not settings.DEBUG) # 速率限制(生产环境)
app.add_middleware(SecurityHeadersMiddleware) # 安全响应头
# 注册路由
app.include_router(health.router, tags=["健康检查"])
app.include_router(
categories.router,
prefix=f"{settings.API_V1_PREFIX}/categories",
tags=["项目分类"]
)
app.include_router(
materials.router,
prefix=f"{settings.API_V1_PREFIX}/materials",
tags=["耗材管理"]
)
app.include_router(
equipments.router,
prefix=f"{settings.API_V1_PREFIX}/equipments",
tags=["设备管理"]
)
app.include_router(
staff_levels.router,
prefix=f"{settings.API_V1_PREFIX}/staff-levels",
tags=["人员级别"]
)
app.include_router(
fixed_costs.router,
prefix=f"{settings.API_V1_PREFIX}/fixed-costs",
tags=["固定成本"]
)
app.include_router(
projects.router,
prefix=f"{settings.API_V1_PREFIX}/projects",
tags=["服务项目"]
)
app.include_router(
market.router,
prefix=f"{settings.API_V1_PREFIX}",
tags=["市场行情"]
)
app.include_router(
pricing.router,
prefix=f"{settings.API_V1_PREFIX}",
tags=["智能定价"]
)
app.include_router(
profit.router,
prefix=f"{settings.API_V1_PREFIX}",
tags=["利润模拟"]
)
app.include_router(
dashboard.router,
prefix=f"{settings.API_V1_PREFIX}",
tags=["仪表盘"]
)
@app.get("/")
async def root():
"""根路径"""
return {
"code": 0,
"message": "success",
"data": {
"name": settings.APP_NAME,
"version": settings.APP_VERSION,
"docs": "/docs" if settings.DEBUG else None
}
}