-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
100 lines (73 loc) · 2.02 KB
/
server.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
const express = require('express')
, app = express()
, Ajv = require('ajv')
, bodyParser = require('body-parser')
, util = require('util')
, fs = require('fs')
, cors = require('cors')
, path = require('path')
, NodeRSA = require('node-rsa')
, server = require('http').createServer(app)
, cmd = require('node-cmd')
, io = require('socket.io')(server);
// Constants
const PORT = 8080;
var tollSchema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"Plaza_Name": {
"type": ["string"]
},
"Interval_Starting": {
"type": ["integer","null"]
},
"Pricing_Module": {
"type": ["string","null"]
},
"Message_Module": {
"type": ["string","null"]
}
},
"required": [
"Plaza_Name", "Interval_Starting", "Pricing_Module", "Message_Module"
]
}
};
app.use(bodyParser.json());
app.use(cors());
var key_data = fs.readFileSync('keys/expresslanesapi.key.pem', 'utf8');
var privatekey = new NodeRSA(key_data);
var ajv = Ajv();
var validate = ajv.compile(tollSchema);
var tolldata = [];
app.get('/', function (req, res) {
res.json(tolldata);
});
app.put('/', function (req, res, next) {
var received = req.body['tolls'];
console.log('PUT received: ', received);
received = privatekey.decrypt(received, 'json');
console.log('decrypted to: ', received);
var valid = validate(received);
if (!valid){
res.status(400).json({ error: validate.errors });
return;
}
tolldata = received;
fs.writeFile("../../live_toll.json", JSON.stringify(tolldata), function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
cmd.run('cd /home/ec2-user/mtlfs_flask_server && /home/ec2-user/miniconda3/envs/flask_server/bin/python3 toll_status_updater.py')
res.status(200).end();
});
io.on('connection', function(socket){
socket.emit('toll', tolldata);
});
server.listen(PORT);
console.log('Running on http://localhost:' + PORT);