forked from maxlyth/shelly-admin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
85 lines (76 loc) · 2.91 KB
/
app.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
/* eslint-disable lodash/prefer-lodash-method */
/* eslint-env node */
/* eslint no-unused-vars: ["error", { "args": "none" }]*/
const path = require('path');
const createError = require('http-errors');
const cookieParser = require('cookie-parser');
const compression = require('compression');
const cors = require('cors');
const proxy = require('express-http-proxy');
const express = require('express');
const authHeader = require('basic-auth-header');
const SSE = require('express-sse');
const morgan = require('morgan');
const ShellyFinder = require('./coapShellyList.js')
const indexRouter = require('./routes/index');
const apiRouter = require('./routes/api');
const app = express();
const sse = new SSE([], { isSerialized: false, initialEvent: 'shellysLoad' });
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(morgan('dev'));
app.use(cors());
app.options('*', cors()) // include before other routes
app.enable('trust proxy', process.env.TRUSTPROXY);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(process.env.PREFIX, express.static(path.join(__dirname, 'public')));
// Add handler for client to be able to request no compression. This is required for express-sse
app.use(compression({
filter: function (req, res) {
return (req.headers['x-no-compression']) ? false : compression.filter(req, res);
}
}));
app.use(path.join(process.env.PREFIX, '/'), indexRouter);
app.use(path.join(process.env.PREFIX, '/api'), apiRouter);
app.use(path.join(process.env.PREFIX, '/proxy/:deviceKey'), proxy(function (req, res) {
const deviceKey = req.params.deviceKey;
const shelly = app.locals.shellylist[deviceKey];
return 'http://' + shelly.ip;
}, {
userResDecorator: function (proxyRes, proxyResData, userReq, userRes) {
let data = proxyResData.toString('utf8');
const regex = /,\s*url:\s*?"\/"\s*?\+\s*?url,/g;
data = data.replaceAll(regex, ', url: ""+url,');
return data;
},
proxyReqOptDecorator: function (proxyReqOpts, srcReq) {
const deviceKey = proxyReqOpts.params.deviceKey;
const shelly = app.locals.shellylist[deviceKey];
if (shelly.auth) {
console.warn('Need to add auth headers for this device');
proxyReqOpts.headers['Authorization'] = authHeader(shelly.shellyuser, shelly.shellypassword);
}
return proxyReqOpts;
}
}
));
app.get(path.join(process.env.PREFIX, '/events'), sse.init);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
const shellycoap = new ShellyFinder(sse);
shellycoap.start(app);
module.exports = app;