-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·74 lines (68 loc) · 2.07 KB
/
index.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
console.log('[*] Loading modules')
var config = require('./config.json'),
express = require('express'),
bodyParser = require('body-parser'),
u = require('./util'),
Database = require('./database').Database,
mosca = require('mosca');
/* METHODS LOAD */
var auth = require('./API/auth');
var iot = require('./API/iot');
/*---*/
/* CREATE MODULES */
console.log('[*] Initializing modules')
var http = express();
var mqtt = new mosca.Server(config.mosca);
var db = new Database();
/*---*/
/* EXPRESS CONFIGURE */
console.log('[*] Configuring express')
http.set('db', db);
http.use(bodyParser.urlencoded({ extended: false }));
http.use(bodyParser.json());
http.use(u.wrapper);
http.use(function (req, res, next) {
req.prepareUser();
var usr = req.query.usr, psw = req.query.psw;
if (usr === undefined || psw === undefined) {
return next();
} else if (usr !== u.trimNonAlpha(usr)) {
res.sendInvalidDataError();
u.exitResolved();
}
var db = req.app.get('db');
db.selectUser(req.query.usr, req.query.psw).then((row) => {
req.fillUser(row);
if (req.user.id === null) {
next();
u.exitResolved();
}
next();
}).catch((err)=>{
return res.handleInternalError(err);
});
});
http.use(auth.uri, auth.router);
http.use(iot.uri, iot.router);
/*---*/
/* MQTT CONFIGURE */
console.log('[*] Configuring MOSCA')
/*mqtt.on('clientConnected', function(client) {
console.log('[MOSCA] client connected', client.id);
});*/
mqtt.on('ready', function() {
mqtt.authenticate = auth.mqtt.authenticate(db);
mqtt.authorizePublish = auth.mqtt.authorizePublish(db);
mqtt.authorizeSubscribe = auth.mqtt.authorizeSubscribe(db);
});
/*---*/
/* DB CONFIGURE */
console.log('[*] Configuring database')
db.configure(config.database);
/*---*/
/* TURN ON ENGINE AND FLY AWAY! */
console.log('[*] Deploying...')
db.connect();
http.listen(config.port);
iot.balance(db, mqtt, 5000)
/*---*/