Skip to content

Commit

Permalink
suport bottle and starlette
Browse files Browse the repository at this point in the history
  • Loading branch information
PWZER committed Apr 26, 2020
1 parent 07a47e7 commit 199fa0d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
18 changes: 18 additions & 0 deletions examples/bottle_test.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion examples/conf/test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
swagger: '2.0'
host: '127.0.0.1:8000'
# host: '127.0.0.1:8989'
schemes:
- http
basePath: /
Expand Down
28 changes: 28 additions & 0 deletions examples/starlette_test.py
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion examples/tornado_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
25 changes: 11 additions & 14 deletions swagger_ui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'/')
Expand Down Expand Up @@ -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'/<filepath:re:.*\.(js|css)>')
@self._app.get(self._uri(r'/swagger.json'))
def config_handler():
return self.get_config(request.urlparts.netloc)

@self._app.get(self._uri(r'/<filepath>'))
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

Expand Down Expand Up @@ -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')

Expand Down

0 comments on commit 199fa0d

Please sign in to comment.