You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.5 KiB
51 lines
1.5 KiB
import os
|
|
|
|
from app.api.ShortCircuit import app_start
|
|
from app.conf.Setting import settings
|
|
import uvicorn
|
|
from fastapi import Request
|
|
from app.api import create_app
|
|
from app.lib.Utils import Utils
|
|
from fastapi.encoders import jsonable_encoder
|
|
from fastapi.exceptions import RequestValidationError
|
|
from fastapi.responses import JSONResponse
|
|
from app.models import DateEntity
|
|
from app.models.Repository import OrmBase
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
app = create_app()
|
|
DateEntity.Base.metadata.create_all(bind=OrmBase.engine)#创建表结构
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
# allow_origins=["*"], # 允许所有源,也可以指定具体源
|
|
allow_origins=["http://127.0.0.1:8000"], # 前端的源地址
|
|
allow_credentials=True,
|
|
allow_methods=["*"], # 允许所有方法
|
|
allow_headers=["*"], # 允许所有头
|
|
)
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
|
return JSONResponse(
|
|
status_code=200,
|
|
content=jsonable_encoder(Utils.false_return(desc='参数错误')),
|
|
)
|
|
|
|
|
|
@app.middleware("http")
|
|
async def before_request(request, call_next):
|
|
response = await call_next(request)
|
|
return response
|
|
|
|
|
|
# @app.on_event("startup")
|
|
# async def startup():
|
|
# # pass
|
|
# await app_start()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
uvicorn.run(app="main:app",host=settings.SERVER_HOST,port=settings.SERVER_POST,reload=settings.RELOAD)
|