Connexion Cookbook¶
This page provides recipes with Connexion as an ingredient.
CORS¶
You can enable CORS (Cross-origin resource sharing) by leveraging the CORSMiddleware offered by
Starlette. You can add it to your application, ideally in front of the RoutingMiddleware.
from pathlib import Path
from connexion import AsyncApp
from connexion.middleware import MiddlewarePosition
from starlette.middleware.cors import CORSMiddleware
app = AsyncApp(__name__)
app.add_middleware(
CORSMiddleware,
position=MiddlewarePosition.BEFORE_EXCEPTION,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_api("openapi.yaml")
if __name__ == "__main__":
app.run(f"{Path(__file__).stem}:app", port=8080)
View a detailed reference of the add_middleware method
- AsyncApp.add_middleware(middleware_class: Type[Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]]], position: MiddlewarePosition = MiddlewarePosition.BEFORE_CONTEXT, **options: Any) None
Add a middleware to the stack on the specified position.
- Parameters:
middleware_class – Middleware class to add
position – Position to add the middleware, one of the MiddlewarePosition Enum
options – Options to pass to the middleware_class on initialization
from pathlib import Path
from connexion import FlaskApp
from connexion.middleware import MiddlewarePosition
from starlette.middleware.cors import CORSMiddleware
app = FlaskApp(__name__)
app.add_middleware(
CORSMiddleware,
position=MiddlewarePosition.BEFORE_EXCEPTION,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_api("openapi.yaml")
if __name__ == "__main__":
app.run(f"{Path(__file__).stem}:app", port=8080)
View a detailed reference of the add_middleware method
- FlaskApp.add_middleware(middleware_class: Type[Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]]], position: MiddlewarePosition = MiddlewarePosition.BEFORE_CONTEXT, **options: Any) None
Add a middleware to the stack on the specified position.
- Parameters:
middleware_class – Middleware class to add
position – Position to add the middleware, one of the MiddlewarePosition Enum
options – Options to pass to the middleware_class on initialization
from pathlib import Path
from asgi_framework import App
from connexion import ConnexionMiddleware
from starlette.middleware.cors import CORSMiddleware
app = App(__name__)
app = ConnexionMiddleware(app)
app.add_middleware(
CORSMiddleware,
position=MiddlewarePosition.BEFORE_EXCEPTION,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_api("openapi.yaml")
if __name__ == "__main__":
app.run(f"{Path(__file__).stem}:app", port=8080)
View a detailed reference of the add_middleware method
- ConnexionMiddleware.add_middleware(middleware_class: Type[Callable[[MutableMapping[str, Any], Callable[[], Awaitable[MutableMapping[str, Any]]], Callable[[MutableMapping[str, Any]], Awaitable[None]]], Awaitable[None]]], *, position: MiddlewarePosition = MiddlewarePosition.BEFORE_CONTEXT, **options: Any) None
Add a middleware to the stack on the specified position.
- Parameters:
middleware_class – Middleware class to add
position – Position to add the middleware, one of the MiddlewarePosition Enum
options – Options to pass to the middleware_class on initialization
Reverse Proxy¶
When running behind a reverse proxy with stripped path prefix, you need to configure your application to properly handle this.
Single known path prefix¶
If there is only a single known prefix your application will be running behind, you can simply pass this path prefix as the root_path to your ASGI server:
$ uvicorn run:app --root-path <root_path>
$ gunicorn -k uvicorn.workers.UvicornWorker run:app --root-path <root_path>
Dynamic path prefix¶
If you are running behind multiple proxies, or the path is not known, you can wrap your application in a ReverseProxied middleware as shown in this example.