-
-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcf4c63
commit cb148b1
Showing
3 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,7 @@ Documentation | |
middleware | ||
testing | ||
cli | ||
cookbook | ||
v3 | ||
|
||
Recommended resources | ||
|