2024-11-19 11:28:05 +00:00
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
|
|
2024-11-20 08:25:36 +00:00
|
|
|
|
from fastapi import FastAPI
|
2024-11-20 10:49:11 +00:00
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2024-11-18 11:47:19 +00:00
|
|
|
|
|
2024-11-19 12:08:21 +00:00
|
|
|
|
from api import login_reg, manage_project, manage_tanant, manage_user
|
2024-11-20 08:25:36 +00:00
|
|
|
|
from database import create_db_and_tables
|
2024-11-18 11:47:19 +00:00
|
|
|
|
|
|
|
|
|
|
# 用于生成和验证JWT的密钥
|
|
|
|
|
|
SECRET_KEY = "your_secret_key"
|
|
|
|
|
|
ALGORITHM = "HS256"
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-19 11:28:05 +00:00
|
|
|
|
# @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)
|
2024-11-20 10:49:11 +00:00
|
|
|
|
|
|
|
|
|
|
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"]
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2024-11-19 12:08:21 +00:00
|
|
|
|
app.include_router(login_reg.router)
|
|
|
|
|
|
app.include_router(manage_tanant.router)
|
|
|
|
|
|
app.include_router(manage_user.router)
|
|
|
|
|
|
app.include_router(manage_project.router)
|