-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
87 lines (81 loc) · 2.34 KB
/
backend.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
from flask import Flask, request, jsonify, render_template, abort
from flask_mail import Mail
import json
from helper import InputParser, Token, Dedup
import db as database
from mail import getMail
from generator import FFmapConfig, FastdConfig, aliasMap
app = Flask(__name__)
mail = Mail(app)
parser = InputParser()
token = Token()
db = database.DB()
dedup = Dedup()
ffmap = FFmapConfig()
fastd = FastdConfig()
alias = aliasMap()
@app.route('/')
def main_site():
return render_template('index.html')
@app.route('/api/node', methods=['POST'])
def process_new():
# check for invalid data
val = parser.getData(request)
vres = parser.validate(parser.getNodeRegex(), val)
if vres:
resp = jsonify(**vres)
resp.status_code = 400
return resp
# check for duplicates
ddres = dedup.checkDups(val['hostname'], val['mac'], val['key'], None)
if ddres:
resp = jsonify(**ddres)
resp.status_code = 409
return resp
# if we reach this part the data should be correct
resp = val
resp['token'] = token.getToken()
db.addNode(resp)
ffmap.genAliasJson()
fastd.genFastdConf()
alias.genAliasMap()
mail.send(getMail(resp))
resp['status'] = 'success'
return jsonify(**resp)
@app.route('/api/node/<tok>', methods=['GET'])
def process_get(tok):
vres = parser.validate(parser.getTokenRegex(), {'token': tok})
if vres:
resp = jsonify(**vres)
resp.status_code = 400
return resp
tres = token.checkToken(tok)
if tres:
resp = jsonify(**tres)
resp.status_code = 404
return resp
return jsonify(**db.getNode(tok))
@app.route('/api/node/<tok>', methods=['PUT'])
def process_update(tok):
val = parser.getData(request)
val.update({'token': tok})
vres = parser.validate(parser.getNodeWithTokenRegex(), val)
if vres:
resp = jsonify(**vres)
resp.status_code = 400
return resp
ddres = dedup.checkDups(val['hostname'], val['mac'], val['key'], val['token'])
if ddres:
resp = jsonify(**ddres)
resp.status_code = 409
return resp
db.updateNode(val)
ffmap.genAliasJson()
fastd.genFastdConf()
alias.genAliasMap()
resp = val
resp['status'] = 'success'
return jsonify(**resp)
if __name__ == "__main__":
app.run(debug=True)