mirror of
https://github.com/LukeHagar/connexion.git
synced 2025-12-06 12:27:45 +00:00
This PR adds an interface for the ConnexionMiddleware, similar to the
interface of the Connexion Apps.
The Connexion Apps are now a simple wrapper around the
ConnexionMiddleware and framework app, delegating the work to the
middleware. This enables a similar interface and behavior for users when
using either the middleware or apps.
The arguments are repeated everywhere there is a user interface, but are
parsed in a central place. Repeating the arguments is not DRY, but
needed to provide users with IDE autocomplete, typing, etc. They are
parsed in a single `_Options` class, which also provides a mechanism to
set default options on an App level, and override them on the more
granular API level.
This makes the long list of provided parameters a lot more manageable,
so I would like to use it for the `Jsonifier` as well, and re-add the
`debug` and `extra_files` arguments which I have dropped in previous
PRs. I'll submit a separate PR for this.
I renamed the `options` parameter to `swagger_ui_options` since it only
contains swagger UI options. This is a breaking change though, and we'll
need to highlight this upon release.
We still have quite a lot of `App`, `MiddlewareApp`, and abstract
classes. It would be great if we could find a way to reduce those
further, or at least find better naming to make it more clear what each
one does 🙂 .
Finally, I added examples on how the middleware can be used with third
party frameworks under `examples/frameworks`. Currently there's an
example for Starlette and Quart, but this should be easy to extend. They
also show how the `ASGIDecorator` and `StarletteDecorator` from my
previous PR can be used.
=====================
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"
}
}