-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (41 loc) · 1.26 KB
/
server.js
File metadata and controls
48 lines (41 loc) · 1.26 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
// Set view engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Routes
app.get('/', (req, res) => {
res.render('index');
});
app.post('/generate', (req, res) => {
try {
// Expecting req.body to be an array of { targets, labels }
const groups = req.body;
if (!Array.isArray(groups)) throw new Error('Input must be an array');
const config = generateJSONFileSDConfig(groups);
res.json({
success: true,
config: config
});
} catch (error) {
res.status(400).json({
success: false,
error: error.message
});
}
});
function generateJSONFileSDConfig(groups) {
let config = '// Prometheus File Service Discovery Configuration\n// Generated by PFSCGen\n\n';
config += JSON.stringify(groups, null, 2);
return config;
}
app.listen(PORT, () => {
console.log(`PFSCGen server running on http://localhost:${PORT}`);
});