-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
62 lines (49 loc) · 1.74 KB
/
server.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
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const path = require('path');
const { fork } = require('child_process');
const GRAFANA_USER = process.env.GRAFANA_USER;
const GRAFANA_PASSWORD = process.env.GRAFANA_PASSWORD;
const app = express();
const port = process.env.EXPORT_SERVER_PORT || 3001;
if (!GRAFANA_USER || !GRAFANA_PASSWORD) {
console.error('.env file do not seems to be found or missing required fields. Please check README.md for more information. ');
process.exit(1);
}
app.use(express.json());
app.use(cors());
app.use('/output', express.static(path.join(__dirname, 'output')));
app.get('/check-status', (req, res) => {
res.send('Server is running');
});
app.post('/generate-pdf', (req, res) => {
let { url: requestUrl, from, to } = req.body;
if (!requestUrl) {
return res.status(400).send('URL is required');
}
const urlObj = new URL(requestUrl);
if (from && !urlObj.searchParams.has('from')) {
urlObj.searchParams.append('from', from);
}
if (to && !urlObj.searchParams.has('to')) {
urlObj.searchParams.append('to', to);
}
const finalUrl = urlObj.toString();
const script = fork('grafana_pdf.js', [finalUrl, `${GRAFANA_USER}:${GRAFANA_PASSWORD}`]);
script.on('message', (message) => {
if (message.success) {
const pdfPath = message.path;
const pdfUrl = `${req.protocol}://${req.get('host')}/output/${path.basename(pdfPath)}`;
res.json({ pdfUrl });
} else {
res.status(500).send(`Error generating PDF: ${message.error}`);
}
});
script.on('error', (error) => {
res.status(500).send(`Error generating PDF: ${error.message}`);
});
});
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});