-
Notifications
You must be signed in to change notification settings - Fork 0
/
evse-proxy.js
82 lines (71 loc) · 2.33 KB
/
evse-proxy.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
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const { fetchData, sendData } = require('./lib/restCalls');
const { readEVSEConfig } = require('./lib/env');
const { logger } = require('./lib/logger');
const {
connectAuthentication, protect,
} = require('./authenticationConnection');
const appUI = express();
appUI.use(bodyParser.urlencoded({ extended: true }));
appUI.set('trust proxy', () => true);
const corsOptions = {
origin(o, callback) {
callback(null, true);
},
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: true,
credentials: true,
maxAge: 3600,
};
const evseConfig = readEVSEConfig();
appUI.use(cors(corsOptions));
connectAuthentication(appUI, evseConfig);
appUI.get('/*', protect(evseConfig), cors(corsOptions), async (req, res) => {
const curConfig = readEVSEConfig();
try {
let path = '/';
if (req.params[0] !== '') {
path = req.params[0] === '/' ? req.params[0].substring(1) : req.params[0];
}
let query = '';
if (req.query) {
query = Object.keys(req.query).map((k) => `${k}=${req.query[k]}`).join('&');
}
const url = curConfig.evseServer[path];
const resp = await fetchData(`${url || (`${curConfig.evseServer['/']}/${path}`)}${query ? `?${query}` : ''}`);
res.status(200).send(resp.data);
} catch (e) {
logger.error(e);
res.status(200).send(e);
}
});
function bodyToText(body) {
return Object.keys(body).map((k) => `${k}=${body[k]}`).join('&');
}
appUI.post('/*', protect(evseConfig), cors(corsOptions), async (req, res) => {
const curConfig = readEVSEConfig();
try {
let path = '/';
if (req.params[0] !== '') {
path = req.params[0] === '/' ? req.params[0].substring(1) : req.params[0];
}
let query = '';
if (req.query) {
query = Object.keys(req.query).map((k) => `${k}=${req.query[k]}`).join('&');
}
const url = curConfig.evseServer[path];
const resp = await sendData(`${url || (`${curConfig.evseServer['/']}/${path}`)}${query ? `?${query}` : ''}`,
'POST', bodyToText(req.body));
res.status(200).send(resp.data);
} catch (e) {
logger.error(e);
res.status(200).send(e);
}
});
appUI.listen(evseConfig.port || 8011, () => {
logger.info(
`HTTP smartthings phevctl UI listening on port ${evseConfig.port || 8011}`,
);
});