Skip to content

app_factory

mlte/backend/app_factory.py

Web application factory.

create(allowed_origins=[])

Create an instance of the application.

Returns:

Type Description
FastAPI

The app

Source code in mlte/backend/core/app_factory.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def create(allowed_origins: list[str] = []) -> FastAPI:
    """
    Create an instance of the application.
    :return: The app
    """
    app = FastAPI(
        title="MLTE Artifact Store",
        docs_url=f"{settings.API_PREFIX}/docs",
        redoc_url=f"{settings.API_PREFIX}/redoc",
        openapi_url=f"{settings.API_PREFIX}/openapi.json",
    )

    # Inject routes
    app.include_router(api_router, prefix=settings.API_PREFIX)

    # Attach middleware
    # NOTE(Kyle): It is imporant middleware is applied AFTER routes are injected
    if len(allowed_origins) > 0:
        app.add_middleware(
            CORSMiddleware,
            allow_origins=allowed_origins,
            allow_credentials=True,
            allow_methods=["*"],
            allow_headers=["*"],
        )

    # Add proper exception handling for Token responses, to be OAuth compliant.
    app.add_exception_handler(
        HTTPTokenException, json_content_exception_handler  # type: ignore
    )

    return app