Commits

remosu  committed 8045ed4

flask app use extension flask.ext.backmongo

  • Participants
  • Parent commits 89b3113

Comments (0)

Files changed (2)

File backmongo_app.py

-from flask import Flask, request, Response
+from flask import Flask
 
-import backmongo
-
+from flask.ext import backmongo
 
 app = Flask(__name__)
-    
-@app.route("/<collection>/", methods=['GET'])
-@app.route("/<collection>/<id>", methods=['GET'])
-def read(collection, id=None):
-    result = backmongo.read(collection, id)
-    return Response(result, mimetype='application/json')
 
-@app.route("/<collection>/<id>", methods=['DELETE'])
-def delete(collection, id):
-    backmongo.delete(collection, id)
-    return Response("{status: OK}", mimetype='application/json')
+backmongo.init_app(app)
 
-@app.route("/<collection>/", methods=['POST'])
-@app.route("/<collection>/<id>", methods=['PUT'])
-def save(collection, id=None):
-    result = backmongo.save(collection, request.json, id)
-    return Response(result, mimetype='application/json')
 
 if __name__ == "__main__":
-    app.run(debug=True)
+    app.run(debug=True)

File flask_backmongo.py

+from flask import Blueprint, request, Response
+
+import backmongo
+
+bp = Blueprint('api', __name__)
+
+@bp.route("/<collection>/", methods=['GET'])
+@bp.route("/<collection>/<id>", methods=['GET'])
+def read(collection, id=None):
+    result = backmongo.read(collection, id)
+    return Response(result, mimetype='application/json')
+
+@bp.route("/<collection>/<id>", methods=['DELETE'])
+def delete(collection, id):
+    backmongo.delete(collection, id)
+    return Response("{status: OK}", mimetype='application/json')
+
+@bp.route("/<collection>/", methods=['POST'])
+@bp.route("/<collection>/<id>", methods=['PUT'])
+def save(collection, id=None):
+    result = backmongo.save(collection, request.json, id)
+    return Response(result, mimetype='application/json')
+
+def init_app(app, url_prefix=''):
+    app.register_blueprint(bp, url_prefix=url_prefix)