Commit 06a9aeeb authored by jtc42's avatar jtc42
Browse files

Added route to list all routes

parent 10525c9c
Loading
Loading
Loading
Loading
+18 −12
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ TODO: Implement API route to cleanly shut down server
"""

from flask import (
    Flask, render_template)
    Flask, render_template, jsonify)

from flask.logging import default_handler
from serial import SerialException
@@ -12,6 +12,7 @@ from serial import SerialException
from flask_cors import CORS

from openflexure_microscope.api.exceptions import JSONExceptionHandler
from openflexure_microscope.api.utilities import list_routes

from openflexure_microscope import Microscope
from openflexure_microscope.camera.pi import StreamingCamera
@@ -89,15 +90,6 @@ def attach_microscope():

##### WEBAPP ROUTES ######

@app.route('/')
def index():
    """
    API demo app
    """
    return render_template(
        'index_v1.html'
    )

##### API ROUTES ######
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)
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
def cleanup():
    global api_microscope
@@ -138,9 +146,7 @@ def cleanup():
    api_microscope.close()
    logging.debug("App teardown complete.")


atexit.register(cleanup)


if __name__ == "__main__":
    app.run(host='0.0.0.0', port="5000", threaded=True, debug=True, use_reloader=False)
 No newline at end of file
+18 −3
Original line number Diff line number Diff line
import pprint
import logging
from werkzeug.exceptions import BadRequest

from flask import url_for

class JsonPayload:
    def __init__(self, request):
@@ -69,5 +69,20 @@ def get_bool(get_arg):


def list_routes(app):
    """Print available functions."""
    pprint.pprint(list(map(lambda x: repr(x), app.url_map.iter_rules())))
    output = {}
    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
 No newline at end of file