代码拉取完成,页面将自动刷新
同步操作将从 雷子/fastapistuday 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
from fastapi import FastAPI,Request
from aioredis import create_redis_pool, Redis
from routers.user import usersRouter
from routers.course import courseRouter
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
from config import *
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
#
# # 前端页面url
# origins = [
# "http://localhost.tiangolo.com",
# "http://localhost:8080",
# "http://192.168.31.35",
# ]
#
# # 后台api允许跨域
# app.add_middleware(
# CORSMiddleware,
# allow_origins=origins,
# allow_credentials=True,
# allow_methods=["*"],
# allow_headers=["*"],
# )
async def get_redis_pool() -> Redis:
if EVENT=="test":
redis = await create_redis_pool(f"redis://:@" + testredishost + ":" + testredisport + "/" + testredisdb + "?encoding=utf-8")
else:
redis = await create_redis_pool(f"redis://:@" + redishost + ":" + redisport + "/" + redisdb + "?encoding=utf-8")
return redis
@app.on_event("startup")
async def startup_event():
app.state.redis = await get_redis_pool()
@app.on_event("shutdown")
async def shutdown_event():
app.state.redis.close()
await app.state.redis.wait_closed()
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
return JSONResponse(
content=jsonable_encoder({"message": exc.errors(), "code": 421}),
)
app.include_router(usersRouter, prefix="/user", tags=['users'])
app.include_router(courseRouter, prefix='/course', tags=['course'])
if __name__ == '__main__':
import uvicorn
uvicorn.run(app='main:app', host="127.0.0.1", port=8000, reload=True, debug=True)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。