Skip to content

auto_rest.app

The app module provides factory functions and utilities for building and deploying Fast-API applications.

Example: Build and Deploy an API

from auto_rest.app import create_app, run_server

app = create_app(app_title="My Application", app_version="1.2.3")
... # Add endpoints to the application here
run_server(app, host="127.0.0.1", port=8081)

create_app(app_title, app_version)

Create and configure a FastAPI application instance.

This function initializes a FastAPI app with a customizable title, version, and optional documentation routes. It also configures application middleware for CORS policies.

Parameters:

Name Type Description Default
app_title str

The title of the FastAPI application.

required
app_version str

The version of the FastAPI application.

required

Returns:

Name Type Description
FastAPI FastAPI

A configured FastAPI application instance.

Source code in auto_rest/app.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def create_app(app_title: str, app_version: str) -> FastAPI:
    """Create and configure a FastAPI application instance.

    This function initializes a FastAPI app with a customizable title, version,
    and optional documentation routes. It also configures application middleware
    for CORS policies.

    Args:
        app_title: The title of the FastAPI application.
        app_version: The version of the FastAPI application.

    Returns:
        FastAPI: A configured FastAPI application instance.
    """

    app = FastAPI(
        title=app_title,
        version=app_version,
        docs_url="/docs/",
        redoc_url=None,
    )

    app.middleware("http")(logging_middleware)
    app.add_middleware(
        CORSMiddleware,
        allow_credentials=True,
        allow_origins=["*"],
        allow_methods=["*"],
        allow_headers=["*"],
    )

    return app

logging_middleware(request, call_next) async

FastAPI middleware for the logging response status codes.

Parameters:

Name Type Description Default
request Request

The incoming HTTP request.

required
call_next callable

The next middleware in the middleware chain.

required

Returns:

Type Description
Response

The outgoing HTTP response.

Source code in auto_rest/app.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
async def logging_middleware(request: Request, call_next: callable) -> Response:
    """FastAPI middleware for the logging response status codes.

    Args:
        request: The incoming HTTP request.
        call_next: The next middleware in the middleware chain.

    Returns:
        The outgoing HTTP response.
    """

    response = await call_next(request)
    level = logging.INFO if response.status_code < 400 else logging.ERROR
    logger.log(level, f"{request.method} ({response.status_code}) {request.client.host} - {request.url.path}")
    return response

run_server(app, host, port)

Deploy a FastAPI application server.

Parameters:

Name Type Description Default
app FastAPI

The FastAPI application to run.

required
host str

The hostname or IP address for the server to bind to.

required
port int

The port number for the server to listen on.

required
Source code in auto_rest/app.py
80
81
82
83
84
85
86
87
88
89
90
def run_server(app: FastAPI, host: str, port: int) -> None:  # pragma: no cover
    """Deploy a FastAPI application server.

    Args:
        app: The FastAPI application to run.
        host: The hostname or IP address for the server to bind to.
        port: The port number for the server to listen on.
    """

    # Uvicorn overwrites its logging level when run and needs to be manually disabled here.
    uvicorn.run(app, host=host, port=port, log_level=1000)