from contextlib import asynccontextmanager from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from api import login_reg, manage_project, manage_tanant, manage_user from database import create_db_and_tables # 用于生成和验证JWT的密钥 SECRET_KEY = "your_secret_key" ALGORITHM = "HS256" # @app.on_event("startup") # def on_startup(): # create_db_and_tables() @asynccontextmanager async def lifespan(app: FastAPI): create_db_and_tables() yield app = FastAPI(lifespan=lifespan) app.add_middleware( CORSMiddleware, allow_origins=["*"], # 允许所有来源,也可以指定具体的来源,例如 ["http://example.com", "https://example.com"] allow_credentials=True, # 允许携带凭证(如cookies) allow_methods=["*"], # 允许所有方法,也可以指定具体的方法,例如 ["GET", "POST", "PUT", "DELETE"] allow_headers=["*"], # 允许所有头部,也可以指定具体的头部,例如 ["Content-Type", "Authorization"] ) app.include_router(login_reg.router) app.include_router(manage_tanant.router) app.include_router(manage_user.router) app.include_router(manage_project.router)