-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (50 loc) · 1.69 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
const puppeteer = require('puppeteer');
var express = require('express');
var app = express();
app.use(express.json());
app.use(express.urlencoded());
async function renderImage(apiKey, panelUrl) {
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on("request", (request) => {
const headers = request.headers();
headers["Authorization"] = "Bearer " + apiKey;
request.continue({
headers,
});
});
await page.goto(panelUrl);
await page.waitForFunction(
() => {
const panelCount = document.querySelectorAll(".panel").length || document.querySelectorAll(".panel-container").length;
return window.panelsRendered >= panelCount;
},
{
timeout: 25 * 1000,
},
);
const imageData = await page.screenshot({ type: "png" });
await browser.close();
return imageData;
}
app.post("/render", function (req, res) {
let apiKey = req.body.apiKey;
let panelUrl = req.body.panelUrl;
console.log("Rendering grafana panel: ", panelUrl)
renderImage(apiKey, panelUrl)
.then((imageData) => {
console.log("Grafana panel rendered successfully: ", panelUrl)
res.end(imageData);
}).catch((err) => {
console.log("Failed to render panel %s", panelUrl, err)
res.status(500).send(err)
});
})
var server = app.listen(8281, function () {
var host = server.address().address
var port = server.address().port
console.log("Grafana rendering app listening at http://%s:%s", host, port)
})