Skip to content

Commit f2c1c21

Browse files
committed
Fix swagger configs and behind proxy behaviour
1 parent a99ce2c commit f2c1c21

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

app/controller/restx.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import logging as log
2+
import os
23

34
from flask_restx import Api
45
from flask_swagger_ui import get_swaggerui_blueprint
6+
from werkzeug.middleware.proxy_fix import ProxyFix
57

68
api = Api(version='1.0', title='Python REST API',
79
description='Template for building Python REST APIs')
@@ -15,16 +17,19 @@ def default_error_handler(_):
1517

1618

1719
def init(app):
18-
# app.config['SERVER_NAME'] = server_name + ':' + str(cfg.get('port', 5000))
20+
app.wsgi_app = ProxyFix(app.wsgi_app)
1921
app.config['SWAGGER_UI_DOC_EXPANSION'] = 'list'
20-
app.config['RESTPLUS_VALIDATE'] = True
21-
app.config['RESTPLUS_MASK_SWAGGER'] = False
22+
app.config['RESTX_VALIDATE'] = True
23+
app.config['RESTX_MASK_SWAGGER'] = False
2224
app.config['ERROR_404_HELP'] = False
2325

24-
api_root = ''
25-
swagger_url = f"{api_root}/api/docs" # URL for exposing Swagger UI (without trailing '/')
26+
server_name = os.environ.get('BACKEND_API_URL', None)
27+
if server_name is not None:
28+
app.config['SERVER_NAME'] = server_name
29+
30+
swagger_url = f"/docs" # URL for exposing Swagger UI (without trailing '/')
2631
log.debug("Registering swagger ui: {}".format(swagger_url))
27-
api_url = f"{api_root}/swagger.json" # Our API url (can of course be a local resource)
32+
api_url = f"/swagger.json" # Our API url (can of course be a local resource)
2833
log.debug("Registering swagger api: %s", api_url)
2934

3035
# Call factory function to create our blueprint

0 commit comments

Comments
 (0)