diff --git a/examples/bottle_test.py b/examples/bottle_test.py new file mode 100644 index 0000000..5c4a80c --- /dev/null +++ b/examples/bottle_test.py @@ -0,0 +1,18 @@ +import os +from bottle import Bottle, run + +app = Bottle() + + +@app.route('/hello/world') +def hello(): + return 'Hello World!!!' + + +if __name__ == '__main__': + cur_dir = os.path.dirname(os.path.abspath(__file__)) + config_path = os.path.join(cur_dir, 'conf/test.yaml') + + from swagger_ui import api_doc + api_doc(app, config_path=config_path, url_prefix='/api/doc') + run(app, host='0.0.0.0', port=8989, debug=True) diff --git a/examples/conf/test.yaml b/examples/conf/test.yaml index eeccb52..bd37924 100644 --- a/examples/conf/test.yaml +++ b/examples/conf/test.yaml @@ -1,5 +1,5 @@ swagger: '2.0' -host: '127.0.0.1:8000' +# host: '127.0.0.1:8989' schemes: - http basePath: / diff --git a/examples/starlette_test.py b/examples/starlette_test.py new file mode 100644 index 0000000..35bfa6d --- /dev/null +++ b/examples/starlette_test.py @@ -0,0 +1,28 @@ +import os +import uvicorn + +from starlette.applications import Starlette +from starlette.routing import Route +from starlette.responses import PlainTextResponse + + +def hello_world(request): + return PlainTextResponse('Hello World!!!') + + +def startup(): + cur_dir = os.path.dirname(os.path.abspath(__file__)) + config_path = os.path.join(cur_dir, 'conf/test.yaml') + + from swagger_ui import api_doc + api_doc(app, config_path=config_path) + + +routes = [ + Route('/hello/world', hello_world), +] + +app = Starlette(debug=True, routes=routes, on_startup=[startup]) + +if __name__ == '__main__': + uvicorn.run("starlette_test:app", host="0.0.0.0", port=8989, log_level="info") diff --git a/examples/tornado_test.py b/examples/tornado_test.py index 02df8f5..ef75b35 100644 --- a/examples/tornado_test.py +++ b/examples/tornado_test.py @@ -21,7 +21,7 @@ def make_app(): config_path = os.path.join(cur_dir, 'conf/test.yaml') from swagger_ui import api_doc - api_doc(app, config_path=config_path) + api_doc(app, config_path=config_path, url_prefix='/api/doc/') app.listen(8989) tornado.ioloop.IOLoop.current().start() diff --git a/swagger_ui/core.py b/swagger_ui/core.py index 0d1196a..d39f094 100644 --- a/swagger_ui/core.py +++ b/swagger_ui/core.py @@ -119,7 +119,7 @@ def _flask_handler(self): swagger_blueprint = Blueprint( 'swagger_blueprint', __name__, url_prefix=self._url_prefix, - static_folder=self.static_dir, static_url_path='/' + static_folder=str(self.static_dir), static_url_path='/' ) @swagger_blueprint.route(r'/') @@ -158,26 +158,22 @@ async def swagger_config_handler(request): self._app.router.add_static(self._uri('/'), path='{}/'.format(self.static_dir)) def _bottle_handler(self): - app = self._app + from bottle import static_file, request - @app.get('/') + @self._app.get(self._uri(r'/')) def index(): return self.doc_html - @app.get(r'/') + @self._app.get(self._uri(r'/swagger.json')) + def config_handler(): + return self.get_config(request.urlparts.netloc) + + @self._app.get(self._uri(r'/')) def java_script_file(filepath): return static_file(filepath, root=self.static_dir) - @app.get('/swagger.json') - def config_handler(): - from bottle import redirect - # If bottle runs with the python 3.6 built-in wsgi-server, - # the sub-request will cause the wsgi server to fail the request, - # so redirecting will work. The host most be set in the config. - redirect(self._config_url) - if self._editor: - @app.get('/editor') + @self._app.get(self._uri('/editor')) def editor(): return self.editor_html @@ -274,7 +270,8 @@ async def swagger_editor_handler(request): return JSONResponse(content=self.editor_html, media_type='text/html') async def swagger_config_handler(request): - return JSONResponse(self.get_config(request.client.host)) + host = '{}:{}'.format(request.url.hostname, request.url.port) + return JSONResponse(self.get_config(host)) self._app.router.add_route(self._uri('/',), swagger_doc_handler, ['get'], 'swagger-ui')