-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestService.py
68 lines (56 loc) · 2.48 KB
/
RestService.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from flask import Flask, url_for, request, Response
import json
import logging
from bson.json_util import dumps
from random import randint
import datetime
class RestService:
"""Hosts rest service for adding and retrieving devices and context"""
app = Flask(__name__)
db = None
@app.route('/')
def api_root():
"""Returns GitHub directory to help with docs."""
return 'Read <a href="https://github.com/lyndon160/Siren-Discovery"> https://github.com/lyndon160/Siren-Discovery </a> for RESTful documentation'
@app.route('/nodes', methods=['GET'])
def api_nodes():
"""Returns all devices and context"""
data = dumps(db.get_nodes())
resp = Response(data)
resp.headers.add('Access-Control-Allow-Origin', '*')
resp.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
resp.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return resp
@app.route('/nodes/register_node', methods=['POST', 'GET'])
def api_register_node():
"""Adds single new node to the database. This includes the nodes IP address"""
logging.debug(request.json)
# TODO add more checking for different fields
if not request.json or 'ip' not in request.json:
logging.warning("Missing information of provision %s", request.json)
resp = Response("Error, did not include correct request information", status=400,
mimetype='application/json')
return resp
data = request.json
data['time'] = datetime.datetime.now()
data['remote_ip'] = request.remote_addr
data['docker_port'] = 2375
db.add_node(data)
logging.info("Node added to db %s", data)
resp = Response("Node added", status=200, mimetype='application/json')
return resp
@app.route('/reset_all', methods=['GET'])
def api_reset_all():
"""Flushes all records in database"""
db.drop_nodes()
resp = Response(json.dumps("Database reset"))
resp.headers.add('Access-Control-Allow-Origin', '*')
resp.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
resp.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return resp
def __init__(self, database):
global db
self.app.use_reloader = False
db = database
# self.app.debug = True
self.app.run(host='0.0.0.0', port=61112, threaded=True)