Added route to list all routes
This commit is contained in:
parent
10525c9c8e
commit
06a9aeeba5
2 changed files with 36 additions and 15 deletions
|
|
@ -4,7 +4,7 @@ TODO: Implement API route to cleanly shut down server
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from flask import (
|
from flask import (
|
||||||
Flask, render_template)
|
Flask, render_template, jsonify)
|
||||||
|
|
||||||
from flask.logging import default_handler
|
from flask.logging import default_handler
|
||||||
from serial import SerialException
|
from serial import SerialException
|
||||||
|
|
@ -12,6 +12,7 @@ from serial import SerialException
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
|
|
||||||
from openflexure_microscope.api.exceptions import JSONExceptionHandler
|
from openflexure_microscope.api.exceptions import JSONExceptionHandler
|
||||||
|
from openflexure_microscope.api.utilities import list_routes
|
||||||
|
|
||||||
from openflexure_microscope import Microscope
|
from openflexure_microscope import Microscope
|
||||||
from openflexure_microscope.camera.pi import StreamingCamera
|
from openflexure_microscope.camera.pi import StreamingCamera
|
||||||
|
|
@ -89,15 +90,6 @@ def attach_microscope():
|
||||||
|
|
||||||
##### WEBAPP ROUTES ######
|
##### WEBAPP ROUTES ######
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
def index():
|
|
||||||
"""
|
|
||||||
API demo app
|
|
||||||
"""
|
|
||||||
return render_template(
|
|
||||||
'index_v1.html'
|
|
||||||
)
|
|
||||||
|
|
||||||
##### API ROUTES ######
|
##### API ROUTES ######
|
||||||
from openflexure_microscope.api.v1 import blueprints
|
from openflexure_microscope.api.v1 import blueprints
|
||||||
|
|
||||||
|
|
@ -121,6 +113,22 @@ app.register_blueprint(plugin_blueprint, url_prefix=uri('/plugin', 'v1'))
|
||||||
task_blueprint = blueprints.task.construct_blueprint(api_microscope)
|
task_blueprint = blueprints.task.construct_blueprint(api_microscope)
|
||||||
app.register_blueprint(task_blueprint, url_prefix=uri('/task', 'v1'))
|
app.register_blueprint(task_blueprint, url_prefix=uri('/task', 'v1'))
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
"""
|
||||||
|
API demo app
|
||||||
|
"""
|
||||||
|
return render_template(
|
||||||
|
'index_v1.html'
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.route('/routes')
|
||||||
|
def routes():
|
||||||
|
"""
|
||||||
|
List of all connected API routes
|
||||||
|
"""
|
||||||
|
return jsonify(list_routes(app))
|
||||||
|
|
||||||
# Automatically clean up microscope at exit
|
# Automatically clean up microscope at exit
|
||||||
def cleanup():
|
def cleanup():
|
||||||
global api_microscope
|
global api_microscope
|
||||||
|
|
@ -138,9 +146,7 @@ def cleanup():
|
||||||
api_microscope.close()
|
api_microscope.close()
|
||||||
logging.debug("App teardown complete.")
|
logging.debug("App teardown complete.")
|
||||||
|
|
||||||
|
|
||||||
atexit.register(cleanup)
|
atexit.register(cleanup)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host='0.0.0.0', port="5000", threaded=True, debug=True, use_reloader=False)
|
app.run(host='0.0.0.0', port="5000", threaded=True, debug=True, use_reloader=False)
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import pprint
|
import pprint
|
||||||
import logging
|
import logging
|
||||||
from werkzeug.exceptions import BadRequest
|
from werkzeug.exceptions import BadRequest
|
||||||
|
from flask import url_for
|
||||||
|
|
||||||
class JsonPayload:
|
class JsonPayload:
|
||||||
def __init__(self, request):
|
def __init__(self, request):
|
||||||
|
|
@ -69,5 +69,20 @@ def get_bool(get_arg):
|
||||||
|
|
||||||
|
|
||||||
def list_routes(app):
|
def list_routes(app):
|
||||||
"""Print available functions."""
|
output = {}
|
||||||
pprint.pprint(list(map(lambda x: repr(x), app.url_map.iter_rules())))
|
for rule in app.url_map.iter_rules():
|
||||||
|
|
||||||
|
options = {}
|
||||||
|
for arg in rule.arguments:
|
||||||
|
options[arg] = "[{0}]".format(arg)
|
||||||
|
|
||||||
|
endpoint = rule.endpoint
|
||||||
|
methods = list(rule.methods)
|
||||||
|
url = url_for(rule.endpoint, **options)
|
||||||
|
line = {
|
||||||
|
'endpoint': endpoint,
|
||||||
|
'methods': methods
|
||||||
|
}
|
||||||
|
output[url] = line
|
||||||
|
|
||||||
|
return output
|
||||||
Loading…
Add table
Add a link
Reference in a new issue