-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (44 loc) · 1.33 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
const fs = require('fs')
const express = require('express');
var cors = require('cors');
const exec = require('child_process').exec;
const app = express();
const https = require('https')
let bodyParser = require('body-parser');
app.use(bodyParser.json({ extended: false }));
app.use(cors());
app.post('/lights', (req, res) => {
const cmdResult = {
result: false
}
console.log(`Psw: ${req.body.psw}`)
console.log(`PowerOn: ${req.body.powerOn}`);
if (req.body.psw != process.env.PSW_API) {
res.json(cmdResult);
return;
}
cmdResult.result = true;
let myShellScript = '';
if (req.body.powerOn == true)
myShellScript = exec('uhubctl/uhubctl -l 2 -a 1');
else
myShellScript = exec('uhubctl/uhubctl -l 2 -a 0');
myShellScript.stdout.on('data', (data)=>{
console.log(data);
});
myShellScript.stderr.on('data', (data)=>{
console.error(data);
});
res.json(cmdResult);
});
https
.createServer(
{
key: fs.readFileSync(`/etc/letsencrypt/live/${process.env.URL_NAME}/privkey.pem`),
cert: fs.readFileSync(`/etc/letsencrypt/live/${process.env.URL_NAME}/cert.pem`),
ca: fs.readFileSync(`/etc/letsencrypt/live/${process.env.URL_NAME}/chain.pem`),
},
app
).listen(443, () => {
console.log('Listening...')
})