54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
import os
|
|
import sys
|
|
|
|
# 添加项目根目录到路径
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from backend.database import init_db
|
|
from backend.routers import channels, notify, history
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Startup
|
|
await init_db()
|
|
yield
|
|
# Shutdown
|
|
|
|
app = FastAPI(
|
|
title="Apprise Notify Center",
|
|
description="多通道通知中心 - 支持 80+ 种通知服务",
|
|
version="1.0.0",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 注册路由
|
|
app.include_router(channels.router)
|
|
app.include_router(notify.router)
|
|
app.include_router(history.router)
|
|
|
|
@app.get("/api/health")
|
|
async def health_check():
|
|
return {"status": "ok"}
|
|
|
|
# 静态文件(前端)- 放在最后
|
|
frontend_path = os.path.join(os.path.dirname(__file__), "..", "frontend")
|
|
if os.path.exists(frontend_path):
|
|
app.mount("/", StaticFiles(directory=frontend_path, html=True), name="frontend")
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run("backend.main:app", host="0.0.0.0", port=8000, reload=True)
|