car/main.py

40 lines
872 B
Python
Raw Normal View History

2023-05-31 21:43:20 +08:00
import os.path
2023-05-31 13:20:04 +08:00
import logger
2023-05-31 11:52:34 +08:00
import uvicorn
2023-05-31 21:43:20 +08:00
from fastapi import FastAPI
2023-05-31 14:31:13 +08:00
from fastapi.middleware.cors import CORSMiddleware
2023-05-31 21:43:20 +08:00
from fastapi.staticfiles import StaticFiles
2023-05-31 11:52:34 +08:00
from handler import car, health
# 禁用 redoc
app = FastAPI(docs_url="/docs", redoc_url=None)
2023-05-31 14:31:13 +08:00
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
2023-05-31 11:52:34 +08:00
app.include_router(health.router)
app.include_router(car.router)
2023-05-31 21:43:20 +08:00
frontend = os.path.join(os.path.dirname(__file__), "frontend")
app.mount("/", StaticFiles(directory=frontend, html=True))
2023-05-31 13:20:04 +08:00
@app.on_event("startup")
async def startup():
logger.logger.info("服务启动")
@app.on_event("shutdown")
async def shutdown():
logger.logger.info("服务关闭")
2023-05-31 11:52:34 +08:00
if __name__ == '__main__':
uvicorn.run("main:app", host="0.0.0.0", port=5000, reload=True)