49 lines
1.1 KiB
Python
49 lines
1.1 KiB
Python
"""
|
|
语音交互网页 - 主入口
|
|
"""
|
|
|
|
import os
|
|
import uvicorn
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import FileResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
# 导入后端服务
|
|
from server import app as api_app
|
|
|
|
# 主应用
|
|
app = FastAPI(title="Voice Chat Web")
|
|
|
|
# CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 挂载 API
|
|
app.mount("/api", api_app)
|
|
|
|
# 静态文件
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
|
|
@app.get("/")
|
|
async def index():
|
|
"""主页"""
|
|
return FileResponse("static/index.html")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
PORT = int(os.getenv("PORT", "19019"))
|
|
SSL_KEY = os.getenv("SSL_KEY", "key.pem")
|
|
SSL_CERT = os.getenv("SSL_CERT", "cert.pem")
|
|
|
|
# 检查是否有 SSL 证书
|
|
if os.path.exists(SSL_KEY) and os.path.exists(SSL_CERT):
|
|
uvicorn.run(app, host="0.0.0.0", port=PORT, ssl_keyfile=SSL_KEY, ssl_certfile=SSL_CERT)
|
|
else:
|
|
uvicorn.run(app, host="0.0.0.0", port=PORT) |