def get_application() -> FastAPI: application = FastAPI(title=PROJECT_NAME, debug=DEBUG, version=VERSION) application.include_router(api_router, prefix=API_PREFIX) pre_load = False if pre_load: application.add_event_handler("startup", create_start_app_handler(application)) return application
def get_application() -> FastAPI: application = FastAPI(title=config.PROJECT_NAME, debug=config.DEBUG, description=config.API_DESCRIPTION, version=config.VERSION, docs_url=None, redoc_url=None, openapi_url=None) application.add_event_handler("startup", create_start_app_handler(application)) application.add_event_handler("shutdown", create_stop_app_handler(application)) try: application.add_middleware( RequestTracerMiddleware, exporter=AzureExporter( connection_string= f'InstrumentationKey={os.getenv("APPINSIGHTS_INSTRUMENTATIONKEY")}', sampler=ProbabilitySampler(1.0))) except Exception as e: logging.error(f"Failed to add RequestTracerMiddleware: {e}") application.add_middleware(ServerErrorMiddleware, handler=generic_error_handler) application.add_exception_handler(HTTPException, http_error_handler) application.add_exception_handler(RequestValidationError, http422_error_handler) application.include_router(api_router) return application
def get_application() -> FastAPI: application = FastAPI() application.add_event_handler("startup", create_start_app_handler(application)) application.add_event_handler("shutdown", create_stop_app_handler(application)) application.include_router(api_router) return application
def get_application() -> FastAPI: application = FastAPI(title=APP_NAME, debug=DEBUG, version=VERSION) application.include_router(api_router, prefix=API_PREFIX) application.mount("/static", StaticFiles(directory=f"{rootdir}/app/static"), name="static") pre_load = False if pre_load: application.add_event_handler("startup", create_start_app_handler(application)) return application
def get_application() -> FastAPI: application = FastAPI(title=PROJECT_NAME, debug=DEBUG, version=VERSION) # application.add_middleware( CORSMiddleware, allow_origins=ALLOWED_HOSTS or ["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # application.add_event_handler("startup", create_start_app_handler(application)) application.include_router(router, ) return application
def create_app(): app = FastAPI(title=config.PROJECT_NAME, debug=config.DEBUG, version=config.VERSION) app.add_middleware( CORSMiddleware, allow_origins=config.ALLOWED_HOSTS or ["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.mount('/static', StaticFiles(directory='static'), name='static') # 应用启动和关闭时,增加监听事件,用来创建和关闭数据库链接。 app.add_event_handler("startup", create_start_app_handler()) app.add_event_handler("shutdown", create_stop_app_handler()) # 路由 app.include_router(page_routes) app.include_router(api_routes, prefix=config.API_PREFIX) return app