From 897e80b39781df76eeb60dad168905f314908c23 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 25 Mar 2014 22:33:51 -0300 Subject: [PATCH] the api/index is always visible, not a plugable blueprint --- luncho/blueprints/index.py | 37 ------------------------------------- luncho/server.py | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 39 deletions(-) delete mode 100644 luncho/blueprints/index.py diff --git a/luncho/blueprints/index.py b/luncho/blueprints/index.py deleted file mode 100644 index 2200a69..0000000 --- a/luncho/blueprints/index.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python -# -*- encoding: utf-8 -*- - -"""The index blueprint. There is nothing here, we just show a summary of -the current API requests.""" - -from operator import itemgetter - -from flask import Blueprint -from flask import jsonify -from flask import current_app - -index = Blueprint('index', __name__) - - -@index.route('', methods=['GET']) -def show_routes(): - """List the current API""" - routes = [] - for rule in current_app.url_map.iter_rules(): - endpoint = rule.endpoint - if endpoint == 'static': - # the server does not have a static path, but Flask automatically - # registers it. so we just ignore it. - continue - - path = str(rule) - # methods = rule.methods - doc = current_app.view_functions[endpoint].__doc__ - - routes.append([ - path, - doc - ]) - - routes.sort(key=itemgetter(0)) - return jsonify(status='OK', api=routes) diff --git a/luncho/server.py b/luncho/server.py index ebbdbc1..c2c01d7 100644 --- a/luncho/server.py +++ b/luncho/server.py @@ -6,7 +6,10 @@ import json import hmac import datetime +from operator import itemgetter + from flask import Flask +from flask import jsonify from luncho.exceptions import LunchoException @@ -98,17 +101,47 @@ class Group(db.Model): # ---------------------------------------------------------------------- # Blueprints # ---------------------------------------------------------------------- -from blueprints.index import index from blueprints.users import users from blueprints.token import token from blueprints.groups import groups -app.register_blueprint(index, url_prefix='/') app.register_blueprint(token, url_prefix='/token/') app.register_blueprint(users, url_prefix='/user/') app.register_blueprint(groups, url_prefix='/group/') +# ---------------------------------------------------------------------- +# The index is a special case +# ---------------------------------------------------------------------- +@app.route('/', methods=['GET']) +def show_api(): + """Return the list of APIs.""" + routes = [] + + for rule in app.url_map.iter_rules(): + endpoint = rule.endpoint + if endpoint == 'static': + # the server does not have a static path, but Flask automatically + # registers it. so we just ignore it. + continue + + path = str(rule) + # methods = rule.methods + doc = app.view_functions[endpoint].__doc__ + + # make the doc a little more... pretty + summary = doc.split('\n\n')[0] + summary = ' '.join(line.strip() for line in summary.split('\n')) + + routes.append([ + path, + summary + ]) + + routes.sort(key=itemgetter(0)) + return jsonify(status='OK', api=routes) + + # ---------------------------------------------------------------------- # Error management # ----------------------------------------------------------------------