Skip to content

Commit

Permalink
add wsgi.py file
Browse files Browse the repository at this point in the history
  • Loading branch information
dtkav committed Jan 8, 2019
1 parent df2d99e commit 2708701
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
23 changes: 23 additions & 0 deletions connexion/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class ReverseProxied(object):

def __init__(self, app, script_name=None, scheme=None, server=None):
self.app = app
self.script_name = script_name
self.scheme = scheme
self.server = server

def __call__(self, environ, start_response):
script_name = environ.get('HTTP_X_SCRIPT_NAME', '') or self.script_name
if script_name:
environ['SCRIPT_NAME'] = script_name
path_info = environ['PATH_INFO']
if path_info.startswith(script_name):
environ['PATH_INFO_OLD'] = path_info
environ['PATH_INFO'] = path_info[len(script_name):]
scheme = environ.get('HTTP_X_SCHEME', '') or self.scheme
if scheme:
environ['wsgi.url_scheme'] = scheme
server = environ.get('HTTP_X_FORWARDED_SERVER', '') or self.server
if server:
environ['HTTP_HOST'] = server
return self.app(environ, start_response)
44 changes: 44 additions & 0 deletions examples/openapi3/reverseproxy/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Reverse Proxy Example

This example demonstrates how to run a connexion application behind a path-altering reverse proxy.

You can either set the path in your app, or set the ``X-Script-Name`` header.

Running:

.. code-block:: bash
Expand All @@ -12,3 +14,45 @@ Running:
$ ./app.py
Now open your browser and go to http://localhost:8080/reverse_proxied/ui/ to see the Swagger UI.


You can also use the ``X-Script-Name`` header to modify the reverse proxy path.
For example:

.. code-block:: bash
curl -H "X-Script-Name: banana" http://localhost:8080/openapi.json
{
"servers" : [
{
"url" : "banana"
}
],
"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"
}
}

0 comments on commit 2708701

Please sign in to comment.