minor additions

This commit is contained in:
Barak Argaman 2017-08-29 18:41:39 +03:00
parent 3d0d0f71a2
commit 31b39d43d8
3 changed files with 21 additions and 4 deletions

View File

@ -1,6 +1,6 @@
from datetime import datetime
import bson
from bson.json_util import dumps
from flask import Flask, send_from_directory, redirect, make_response
import flask_restful
@ -11,7 +11,8 @@ from cc.resources.telemetry import Telemetry
from cc.resources.monkey_configuration import MonkeyConfiguration
from cc.resources.monkey_download import MonkeyDownload
from cc.resources.netmap import NetMap
from cc.resources.edge import Edge
from cc.resources.edge import Edge, Node
from cc.resources.root import Root
__author__ = 'Barak'
@ -19,6 +20,8 @@ __author__ = 'Barak'
def serve_static_file(path):
print 'requested', path
if path.startswith('api/'):
return make_response(404)
return send_from_directory('ui/dist', path)
@ -45,10 +48,18 @@ def normalize_obj(obj):
return obj
def output_json(obj, code, headers=None):
obj = normalize_obj(obj)
resp = make_response(dumps(obj), code)
resp.headers.extend(headers or {})
return resp
def init_app(mongo_url):
app = Flask(__name__)
api = flask_restful.Api(app)
api.representations = {'application/json': output_json}
app.config['MONGO_URI'] = mongo_url
mongo.init_app(app)
@ -64,6 +75,7 @@ def init_app(mongo_url):
api.add_resource(MonkeyDownload, '/api/monkey/download', '/api/monkey/download/',
'/api/monkey/download/<string:path>')
api.add_resource(NetMap, '/api/netmap', '/api/netmap/')
api.add_resource(Edge, '/api/edge', '/api/edge/')
api.add_resource(Edge, '/api/netmap/edge', '/api/netmap/edge/')
api.add_resource(Node, '/api/netmap/node', '/api/netmap/node/')
return app

View File

@ -22,3 +22,8 @@ class Edge(flask_restful.Resource):
new_edges.append(edges[i])
return {"edges": new_edges}
return {}
class Node(flask_restful.Resource):
def get(self):
pass

View File

@ -16,7 +16,7 @@ class Root(flask_restful.Resource):
action = request.args.get('action')
if not action:
return jsonify(ip=local_ip_addresses()[0], mongo=str(mongo.db))
return jsonify(ips=local_ip_addresses(), mongo=str(mongo.db))
elif action == "reset":
mongo.db.config.drop()