-
Notifications
You must be signed in to change notification settings - Fork 2
/
RouterSmartAppServer.js
179 lines (146 loc) · 5.15 KB
/
RouterSmartAppServer.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const env = require('./lib/env');
const {
getUsers,
assignMacToUser,
removeMacToUser,
removeUser,
assignShard,
addUser,
} = require('./lib/userManager');
const { getListComponents } = require('./lib/componentManager');
const { getAllNetwork, getAllNetworkUI } = require('./lib/modifyNetwork');
const {
presenceMobiles,
blockUserMac,
unBlockUserMac,
presenceMobilesUI,
} = require('./lib/presenceMobile');
const logger = require('./lib/logger');
const { saveSmartThingDeviceInfo } = require('./lib/registerDevice');
const {
getSettings, saveSetting,
} = require('./lib/settingManager');
const {
connectKeycloak, protect,
} = require('./lib/keycloakConnection');
const {
installCrons,
} = require('./lib/cronConnection');
const corsOptions = {
origin(o, callback) {
callback(null, true);
},
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: true,
credentials: true,
maxAge: 3600,
};
const { port } = env.config().server;
const server = express();
server.use(bodyParser.json());
server.use(cors(corsOptions));
connectKeycloak(server);
server.get('/health', cors(corsOptions), (req, res) => {
const status = { status: 'OK' };
res.send(JSON.stringify(status));
});
// server.get('/createGuestNetwork', cors(corsOptions), (req, res) => {
// res.writeHead(200, { 'Content-Type': 'application/json' });
// createNetwork(req, res);
// });
// server.get('/deleteGuestNetwork', cors(corsOptions), (req, res) => {
// res.writeHead(200, { 'Content-Type': 'application/json' });
// deleteNetwork(req, res);
// });
// server.get(`/getGuestNetwork`, (req, res) => {
// res.writeHead(200, { 'Content-Type': 'application/json' });
// getNetwork(req, res);
// });
server.get('/getAllNetwork', cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
getAllNetwork(req, res);
});
server.get('/presenceMobiles', cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
presenceMobiles(req, res);
});
server.post('/blockMac', cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
blockUserMac(req, res);
});
server.post('/unBlockMac', cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
unBlockUserMac(req, res);
});
// server.get('/BlockedMacs', cors(corsOptions), (req, res) => {
// res.writeHead(200, { 'Content-Type': 'application/json' });
// blockedUserMac(req, res);
// });
server.post('/registerDevice', cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
saveSmartThingDeviceInfo(req, res);
});
// BACKEND UI SERVIC9mi8ES
server.use('/', protect(), express.static(`${__dirname}/router-ui/public`));
server.get('/ui/components', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
getListComponents(req, res);
});
server.get('/ui/settings', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
getSettings(req, res);
});
server.post('/ui/settings', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
saveSetting(req, res);
});
server.post('/ui/removeUser', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
removeUser(req, res);
});
server.get('/ui/getUsers', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
getUsers(req, res);
});
server.post('/ui/addUser', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
addUser(req, res);
});
server.get('/ui/networks', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
getAllNetworkUI(req, res);
});
server.get('/ui/presenceMobiles', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
presenceMobilesUI(req, res);
});
server.post('/ui/assignMac', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
assignMacToUser(req, res);
});
server.post('/ui/blockMac', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
blockUserMac(req, res);
});
server.post('/ui/unBlockMac', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
unBlockUserMac(req, res);
});
server.post('/ui/assignShard', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
assignShard(req, res);
});
server.post('/ui/removeMacToUser', protect(), cors(corsOptions), (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
removeMacToUser(req, res);
});
server.listen(port, () => {
logger.info(`HTTP asus-guest-network listening on port ${port}`);
installCrons();
});
process.on('exit', () => {
server.stop();
});