46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from contextlib import asynccontextmanager
|
|
from app.asr_service import asr_router
|
|
from app.monitor_service import monitor_router
|
|
from app.tts_service import tts_router
|
|
import uvicorn
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""生命周期管理"""
|
|
# 服务启动初始化
|
|
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
|
print(" ASR & Monitor Service Start")
|
|
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
|
yield
|
|
# 服务停止清理
|
|
print("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
|
print(" Service Stopped Cleanly")
|
|
print("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━")
|
|
|
|
# 创建应用实例
|
|
app = FastAPI(lifespan=lifespan)
|
|
|
|
# 配置CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # 允许所有来源,也可以指定具体的域名,例如 ["https://example.com"]
|
|
allow_credentials=True, # 是否允许发送 cookies
|
|
allow_methods=["*"], # 允许所有方法,也可以指定具体的方法,例如 ["GET", "POST"]
|
|
allow_headers=["*"], # 允许所有头信息,也可以指定具体的头信息
|
|
)
|
|
|
|
# 挂载子路由
|
|
app.include_router(asr_router, prefix="/asr")
|
|
app.include_router(monitor_router, prefix="/monitor")
|
|
app.include_router(tts_router, prefix="/tts")
|
|
|
|
# 挂载静态文件(可选)
|
|
# app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
# 注意:这里移除了直接运行的代码
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run("app.main:app", host="0.0.0.0", port=9580) |