mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-09 04:19:32 +00:00
This PR updates the examples for Connexion 3.0 and merges them for OpenAPI and Swagger. 2 examples required some changes to make them work: - The reverse proxy example required some fixes to the SwaggerUIMiddleware to leverage the `root_path` correctly. This is included in the PR. - The enforced defaults example requires the json validator to adapt the body and pass it on. We currently pass on the original body after validation, and I'm not sure if we should change this. I'll submit a separate PR to discuss this.
89 lines
2.3 KiB
ReStructuredText
89 lines
2.3 KiB
ReStructuredText
=====================
|
|
Reverse Proxy Example
|
|
=====================
|
|
|
|
This example demonstrates how to run a connexion application behind a path-altering reverse proxy.
|
|
|
|
You can set the path in three ways:
|
|
|
|
- Via the Middleware
|
|
.. code-block::
|
|
|
|
app = ReverseProxied(app, root_path="/reverse_proxied/")
|
|
|
|
- Via the ASGI server
|
|
.. code-block::
|
|
|
|
uvicorn ... --root_path="/reverse_proxied/"
|
|
|
|
- By using the ``X-Forwarded-Path`` header in your proxy server. Eg in nginx:
|
|
.. code-block::
|
|
|
|
location /proxied {
|
|
proxy_pass http://192.168.0.1:5001;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_set_header X-Forwarded-Path /proxied;
|
|
}
|
|
|
|
To run this example, install Connexion from PyPI:
|
|
|
|
.. code-block::
|
|
|
|
$ pip install --upgrade connexion[swagger-ui]
|
|
|
|
and then run it either directly
|
|
.. code-block::
|
|
|
|
$ python app.py
|
|
|
|
or using uvicorn (or another async server):
|
|
.. code-block::
|
|
$ uvicorn --factory app:create_app --port 8080
|
|
|
|
If your proxy server is running at http://localhost:8080/revers_proxied/, you can go to
|
|
http://localhost:8080/reverse_proxied/openapi/ui/ to see the Swagger UI.
|
|
|
|
|
|
Or you can test this using the ``X-Forwarded-Path`` header to modify the reverse proxy path.
|
|
For example, note the servers block:
|
|
|
|
.. code-block:: bash
|
|
|
|
curl -H "X-Forwarded-Path: /banana/" http://localhost:8080/openapi/openapi.json
|
|
|
|
{
|
|
"servers" : [
|
|
{
|
|
"url" : "/banana/openapi"
|
|
}
|
|
],
|
|
"paths" : {
|
|
"/hello" : {
|
|
"get" : {
|
|
"responses" : {
|
|
"200" : {
|
|
"description" : "hello",
|
|
"content" : {
|
|
"text/plain" : {
|
|
"schema" : {
|
|
"type" : "string"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
"operationId" : "app.hello",
|
|
"summary" : "say hi"
|
|
}
|
|
}
|
|
},
|
|
"openapi" : "3.0.0",
|
|
"info" : {
|
|
"version" : "1.0",
|
|
"title" : "Path-Altering Reverse Proxy Example"
|
|
}
|
|
}
|
|
|