12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #!/usr/bin/env python
- """
- @Contact : liuyuqi.gov@msn.cn
- @Time : 2024/03/24 13:48:03
- @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
- @Desc :
- """
- import logging
- from apps.api import api_router
- from apps.config import settings
- from starlette.middleware.cors import CORSMiddleware
- from fastapi import FastAPI
- def init_banner():
- """
- Print banner
- """
- banner = """
- FastAPI Example
- concat me: liuyuqi.gov@msn.cn
- """
- print(banner)
- def init_logging(logger_name: str) -> logging:
- """
- Initialize logger
- """
- logger = logging.getLogger(logger_name)
- logger.setLevel(logging.DEBUG)
- if logger.hasHandlers():
- logger.handlers.clear()
- for handler in self.available_handlers:
- logger.addHandler(handler)
- logger.propagate = False
- return logger
- def init_hook(app: FastAPI):
- @app.on_event("startup")
- async def startup_event():
- print("Application started")
- # await initiate_database()
- @app.on_event("shutdown")
- async def shutdown_event():
- print("Application shutdown")
- # await close_database_connection_pool()
- def init_middlewares(app: FastAPI):
- """Add CORS middleware to allow all origins"""
- if settings.BACKEND_CORS_ORIGINS:
- app.add_middleware(
- CORSMiddleware,
- allow_origins=[
- str(origin).strip("/") for origin in settings.BACKEND_CORS_ORIGINS
- ],
- allow_credentials=True,
- # allow_methods=["*"],
- allow_methods=("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"),
- allow_headers=["*"],
- )
- # @fastapp.middleware('http')
- # async def add_process_time_header(request: Request, call_next): # call_next将接收request请求做为参数
- # start_time = time.time()
- # response = await call_next(request)
- # process_time = time.time() - start_time
- # response.headers['X-Process-Time'] = str(process_time) # 添加自定义的以“X-”开头的请求头
- # return response
- # fastapp.mount(path='/static', app=StaticFiles(directory='./coronavirus/static'), name='static')
- def register_db(app: FastAPI):
- """Register database connection"""
- pass
- # with Session(engine) as session:
- # initate_postgres(session)
- def register_exception_handlers(app: FastAPI):
- """Register exception handlers"""
- pass
- def register_routes(app: FastAPI):
- """Register all routes"""
- app.include_router(api_router)
|