def create_app(): app = FastAPI(title=config.PROJECT_NAME, debug=config.DEBUG, version=config.VERSION, docs_url=None, redoc_url=None) app.add_middleware( CORSMiddleware, allow_origins=config.ALLOWED_HOSTS or ["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) add_timing_middleware(app, record=logger.info, prefix="solr", exclude="untimed") app.add_event_handler("startup", create_start_app_handler()) app.add_event_handler("shutdown", create_stop_app_handler()) # router app.include_router(service_routes, prefix=config.PAGE_PREFIX) # app.include_router(api_routes, prefix=config.API_PREFIX) app.include_router(api_routes) init_logger() from src.scheduler import customized_jobs customized_jobs.add_a_job() return app
import logging from fastapi import FastAPI from fastapi_utils.timing import add_timing_middleware, record_timing from starlette.requests import Request from starlette.staticfiles import StaticFiles from mathematics import fibonacci, factorial, ackermann, InvalidNumber logging.basicConfig(level=logging.INFO, filename='logging.log') logger = logging.getLogger(__name__) app = FastAPI() add_timing_middleware(app, record=logger.info, prefix="app", exclude="untimed") static_files_app = StaticFiles(directory=".") @app.get('/fibonacci') def fib(n: int, request: Request): return request_handler(request, fibonacci, n) @app.get('/factorial') def fact(n: int, request: Request): return request_handler(request, factorial, n) @app.get('/ackermann') def ack(m: int, n: int, request: Request): return request_handler(request, ackermann, m, n)
) # Env vars LOGLEVEL = os.environ.get("LOGLEVEL", "DEBUG") MODEL_INACTIVITY = int(os.environ.get("MODEL_INACTIVITY", "10")) # Setup logging LOGLEVEL = os.environ.get("LOGLEVEL", "DEBUG") logging.config.fileConfig("/app/logging.conf", disable_existing_loggers=False, defaults={"level": LOGLEVEL}) logger = logging.getLogger(__name__) # Setup FastAPI app = FastAPI() add_timing_middleware(app, record=logger.info) triton_client = get_triton_client() model_dict = get_model_dict() @app.on_event("startup") @repeat_every(seconds=10) async def remove_expired_models(): # Get loaded models loaded_models = [ m.get("name") for m in triton_client.get_model_repository_index() if m.get("state", "UNAVAILABLE") == "READY" ] for model in loaded_models: last_active = check_last_active(model)
from pathlib import Path from _pytest.capture import CaptureFixture from fastapi import FastAPI from starlette.requests import Request from starlette.staticfiles import StaticFiles from starlette.testclient import TestClient from fastapi_utils.timing import add_timing_middleware, record_timing app = FastAPI() add_timing_middleware(app, exclude="untimed") static_files_app = StaticFiles(directory=".") app.mount(path="/static", app=static_files_app, name="static") @app.get("/timed") def get_timed() -> None: pass @app.get("/untimed") def get_untimed() -> None: pass client = TestClient(app) def test_timing(capsys: CaptureFixture) -> None: client.get("/timed")