diff --git a/firefly/app.py b/firefly/app.py index a284ac0..c9920be 100644 --- a/firefly/app.py +++ b/firefly/app.py @@ -1,6 +1,7 @@ import cgi from webob import Request, Response from webob.exc import HTTPNotFound +from jinja2 import PackageLoader, Environment import json import logging from .validator import validate_args, ValidationError @@ -22,6 +23,9 @@ ctx = threading.local() ctx.request = None +env = Environment(loader=PackageLoader('firefly', 'templates')) +template = env.get_template('index.html') + class Firefly(object): def __init__(self, auth_token=None): self.mapping = {} @@ -47,6 +51,20 @@ def generate_index(self): } return help_dict + def render_docs(self, **kwargs): + functions = [ + {'name': name, 'path': spec['path'], 'doc': spec['doc'], 'parameters': spec['parameters']} + for name, spec in self.generate_function_list().items() + ] + html = template.render({ + 'host_url': kwargs['host_url'], + 'functions': functions + }) + response = Response(content_type='text/html') + response.status = 200 + response.text = html + return response + def __call__(self, environ, start_response): request = Request(environ) response = self.process_request(request) @@ -73,7 +91,9 @@ def process_request(self, request): ctx.request = request path = request.path_info - if path in self.mapping: + if path == "/docs": + return self.render_docs(host_url=request.environ['HTTP_HOST']) + elif path in self.mapping: func = self.mapping[path] response = func(request) else: diff --git a/firefly/templates/index.html b/firefly/templates/index.html new file mode 100644 index 0000000..fe4f703 --- /dev/null +++ b/firefly/templates/index.html @@ -0,0 +1,78 @@ + + +
+ + + +Version: {{ version }}
+ {% endif %} ++ {% if function.doc %} + {{ function.doc }} + {% else %} + No docstring provided + {% endif %} +
+The API can be used by using firefly-client
+Install it using:
+Usage:
+