CostEvalPlatform/main.py

40 lines
1.3 KiB
Python
Raw Permalink Normal View History

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-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
# 用于生成和验证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
2024-11-20 14:27:00 +00:00
# noinspection PyTypeChecker
2024-11-20 10:49:11 +00:00
app.add_middleware(
CORSMiddleware,
2024-11-20 14:08:21 +00:00
# allow_origins=["*"],
2024-11-20 14:27:00 +00:00
allow_origins=["http://localhost:8080", "http://localhost:5000"],
# 允许所有来源,也可以指定具体的来源,例如 ["http://example.com", "https://example.com"]
2024-11-20 10:49:11 +00:00
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)