-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
72 lines (61 loc) · 2.26 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
var appRoot = require('app-root-path');
var bodyParser = require('body-parser');
var schedule = require('node-schedule');
var express = require('express');
var expressValidator = require('express-validator');
var app = express();
var communicator = appRoot.require('/controllers/communicator.js');
var transmitters = appRoot.require('/controllers/transmitters.js');
var receivers = appRoot.require('/controllers/receivers.js');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(expressValidator({
customValidators: {
gte: function(param, num) {
return param >= num;
},
lte: function(param, num) {
return param <= num;
},
isArrayOfInts: function(param) {
if (typeof param !== "string") return false;
var ints = param.split(",");
return ints.every(function(value){
return !isNaN(value);
});
}
}
}));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
var port = process.env.PORT || 8080;
var router = express.Router();
router.get('/', function(req, res) {
res.json({ message: 'Welcome to the JustAddPower API' });
});
router.get('/transmitters', transmitters.getAll);
router.get('/transmitters/:id', transmitters.getOne);
router.get('/transmitters/:id/receivers', receivers.getForTransmitter);
router.post('/transmitters', transmitters.create);
router.put('/transmitters/:id', transmitters.update);
router.delete('/transmitters/:id', transmitters.delete);
router.get('/receivers', receivers.getAll);
router.get('/receivers/:id', receivers.getOne);
router.post('/receivers', receivers.create);
router.put('/receivers/:id', receivers.update);
router.delete('/receivers/:id', receivers.delete);
app.use('', router);
app.listen(port);
// Save switch running-config to startup-config every hour
schedule.scheduleJob('0 * * * *', function() {
communicator.saveConfig()
.then(function() {
console.log("Switch config saved.");
})
.catch(function(error) {
console.log('Error saving config.', error);
});
});