Skip to content

main

lifespan(fastapi_app) async

Manages the lifespan of a FastAPI application, handling setup during startup and cleanup during shutdown.

This function is meant to be used as an async context manager for setting up and tearing down application-wide resources or configurations in a uniform way.

Parameters:

Name Type Description Default
fastapi_app FastAPI

Instance of the FastAPI application

required

Returns:

Type Description
AsyncIterator[None]

Async context for managing the lifespan of the FastAPI application

Source code in backend/app/main.py
@asynccontextmanager
async def lifespan(fastapi_app: FastAPI):
    """
    Manages the lifespan of a FastAPI application, handling setup during startup and cleanup
    during shutdown.

    This function is meant to be used as an async context manager for setting up and tearing
    down application-wide resources or configurations in a uniform way.

    :param fastapi_app: Instance of the FastAPI application
    :type fastapi_app: FastAPI
    :return: Async context for managing the lifespan of the FastAPI application
    :rtype: AsyncIterator[None]
    """
    # startup event
    yield

root() async

Handles the root endpoint of the FastAPI application, which responds with an HTML page providing a welcome message and links to documentation.

Provides a simple HTML-based response notifying users about the available documentation resources. The background and content are customized with inline CSS styling for user visual experience.

Returns:

Type Description
HTMLResponse

HTMLResponse containing the welcome page content and HTTP status code 200.

Source code in backend/app/main.py
@fastapi_app.get("/", response_class=HTMLResponse)
async def root():
    """
    Handles the root endpoint of the FastAPI application, which responds with an HTML page
    providing a welcome message and links to documentation.

    Provides a simple HTML-based response notifying users about the available documentation
    resources. The background and content are customized with inline CSS styling for user
    visual experience.

    :return: HTMLResponse containing the welcome page content and HTTP status code 200.
    :rtype: HTMLResponse
    """
    html_content = """
    <html>
        <head>
            <title>EnSys FastAPI</title>
        </head>
        <body style="background-color:#dcdcde; margin: auto; width: 75vh; height: 75%; display: flex; justify-content: center; align-items: center;">
        <div style="background-color:#fcf9e8; width:100%; text-align: center; font-family: monospace; padding: 15px">
            <h1>Welcome</h1>
            <p>For documentation see '/docs', '/redoc' or <a target="_blank" href="https://in-ret.github.io/ensys-gui-new/">this link</a>.<p>
        </div>
        </body>
    </html>
    """

    return HTMLResponse(
        content=html_content,
        status_code=status.HTTP_200_OK
    )