-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrestserver.js
128 lines (106 loc) · 3.61 KB
/
restserver.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
const express = require('express');
const fs = require('fs');
const path = require('path');
const https = require('https');
const http = require('http');
const { format, toZonedTime } = require('date-fns-tz');
const timeZone = process.env.TZ || 'UTC';
const PORT = process.env.PORT || 3300;
const USE_HTTPS = process.env.USE_HTTPS === 'true';
const KEY = process.env.KEY_FILE || 'server.key';
const CRT = process.env.CRT_FILE || 'server.crt';
const sslOptions = USE_HTTPS ? {
key: fs.readFileSync(KEY),
cert: fs.readFileSync(CRT)
} : {};
const INPUT_PIPE_PATH = '/tmp/rscp2p.fifo.data';
const OUTPUT_PIPE_PATH = '/tmp/rscp2p.fifo.cmd';
const app = express();
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
let dataStore = {};
const readFromPipe = () => {
const pipeStream = fs.createReadStream(INPUT_PIPE_PATH, { encoding: 'utf8' });
pipeStream.on('data', chunk => {
const lines = chunk.trim().split('\n');
lines.forEach(line => {
const [begin, key, value, unit, end] = line.split('=');
if (begin == "$" && key && value && end == "$") {
const currentDate = new Date();
const zonedDate = toZonedTime(currentDate, timeZone);
const rfc3339Timestamp = format(zonedDate, "yyyy-MM-dd'T'HH:mm:ssXXX", { timeZone });
dataStore[key.trim().replaceAll("/","_")] = {
value: value.trim(),
unit: unit.trim().replaceAll("_",""),
timestamp: rfc3339Timestamp
};
} else {
console.error('Invalid input format');
}
});
});
pipeStream.on('end', () => {
console.log('Pipe closed, reopening...');
setTimeout(readFromPipe, 1000);
});
pipeStream.on('error', (err) => {
console.error('Pipe error:', err);
setTimeout(readFromPipe, 1000);
});
};
readFromPipe();
app.get('/data', (req, res) => {
res.json(dataStore);
});
app.get('/data/:key', (req, res) => {
const key = req.params.key;
if (dataStore[key]) {
res.json({ [key]: dataStore[key] });
} else {
res.status(404).json({ error: 'Key not found' });
}
});
app.get('/data/:key/raw', (req, res) => {
const key = req.params.key;
if (dataStore[key]) {
res.send(dataStore[key].value);
} else {
res.status(404).send('Key not found');
}
});
app.post('/data', (req, res) => {
const { key, value } = req.body;
if (!key || !value) {
return res.status(400).json({ error: 'Key and value are required' });
}
const outputStream = fs.createWriteStream(OUTPUT_PIPE_PATH, { flags: 'a' });
outputStream.write(`${key}=${value}`, (err) => {
if (err) {
console.error('Failed to write to pipe:', err);
return res.status(500).json({ error: 'Failed to write to pipe' });
}
res.json({ message: 'Data sent to rscp2p' });
});
outputStream.end();
});
app.delete('/data/:key', (req, res) => {
const key = req.params.key;
if (dataStore[key]) {
delete dataStore[key];
res.json({ message: `Key '${key}' deleted` });
} else {
res.status(404).json({ error: 'Key not found' });
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
if (USE_HTTPS) {
https.createServer(sslOptions, app).listen(PORT, () => {
console.log(`HTTPS Server is running on port ${PORT}`);
});
} else {
http.createServer(app).listen(PORT, () => {
console.log(`HTTP Server is running on port ${PORT}`);
});
}