Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CORS documentation #1790

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions docs/cookbook.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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``.


.. tab-set::

.. tab-item:: AsyncApp
:sync: AsyncApp

.. code-block:: python
from connexion import AsyncApp
from connexion.middleware import MiddlewarePosition
from starlette.middleware.cors import CORSMiddleware
app = connexion.AsyncApp(__name__)
app.add_middleware(
CORSMiddleware,
position=MiddlewarePosition.BEFORE_ROUTING,
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)
.. dropdown:: View a detailed reference of the ``add_middleware`` method
:icon: eye

.. automethod:: connexion.AsyncApp.add_middleware
:noindex:

.. tab-item:: FlaskApp
:sync: FlaskApp

.. code-block:: python
from connexion import FlaskApp
from connexion.middleware import MiddlewarePosition
from starlette.middleware.cors import CORSMiddleware
app = connexion.FlaskApp(__name__)
app.add_middleware(
CORSMiddleware,
position=MiddlewarePosition.BEFORE_ROUTING,
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)
.. dropdown:: View a detailed reference of the ``add_middleware`` method
:icon: eye

.. automethod:: connexion.FlaskApp.add_middleware
:noindex:

.. tab-item:: ConnexionMiddleware
:sync: ConnexionMiddleware

.. code-block:: python
from asgi_framework import App
from connexion import ConnexionMiddleware
from connexion.lifecycle import ConnexionRequest, ConnexionResponse
app = App(__name__)
app = ConnexionMiddleware(app)
app.add_middleware(
CORSMiddleware,
position=MiddlewarePosition.BEFORE_ROUTING,
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)
.. dropdown:: View a detailed reference of the ``add_middleware`` method
:icon: eye

.. automethod:: connexion.ConnexionMiddleware.add_middleware
:noindex:

.. _CORSMiddleware: https://www.starlette.io/middleware/#corsmiddleware
6 changes: 3 additions & 3 deletions docs/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ problem responses.
app.add_error_handler(FileNotFoundError, not_found)
app.add_error_handler(404, not_found)
.. dropdown:: View a detailed reference of the :code:`add_middleware` method
.. dropdown:: View a detailed reference of the ``add_error_handler`` method
:icon: eye

.. automethod:: connexion.AsyncApp.add_error_handler
Expand Down Expand Up @@ -63,7 +63,7 @@ problem responses.
app.add_error_handler(FileNotFoundError, not_found)
app.add_error_handler(404, not_found)
.. dropdown:: View a detailed reference of the :code:`add_middleware` method
.. dropdown:: View a detailed reference of the ``add_error_handler`` method
:icon: eye

.. automethod:: connexion.FlaskApp.add_error_handler
Expand Down Expand Up @@ -99,7 +99,7 @@ problem responses.
app.add_error_handler(FileNotFoundError, not_found)
app.add_error_handler(404, not_found)
.. dropdown:: View a detailed reference of the :code:`add_middleware` method
.. dropdown:: View a detailed reference of the ``add_error_handler`` method
:icon: eye

.. automethod:: connexion.ConnexionMiddleware.add_error_handler
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Documentation
middleware
testing
cli
cookbook
v3

Recommended resources
Expand Down
Loading