CostEvalPlatform/main.py
2024-11-20 22:27:00 +08:00

40 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
# noinspection PyTypeChecker
app.add_middleware(
CORSMiddleware,
# allow_origins=["*"],
allow_origins=["http://localhost:8080", "http://localhost:5000"],
# 允许所有来源,也可以指定具体的来源,例如 ["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)