-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
60 lines (47 loc) · 1.58 KB
/
index.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
const express = require("express");
const bearerToken = require("express-bearer-token");
const Sentry = require("@sentry/node");
const Tracing = require("@sentry/tracing");
const morgan = require("morgan");
const svgToImg = require("svg-to-img");
const nunjucks = require("nunjucks");
const fs = require("fs").promises;
const app = express();
if (process.env.SENTRY_DSN && process.env.SENTRY_DSN.length) {
Sentry.init({
dsn: process.env.SENTRY_DSN,
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Tracing.Integrations.Express({ app }),
],
});
}
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
app.use(morgan("combined"));
app.use(bearerToken());
app.use((req, res, next) => {
if (process.env.SECRET_TOKEN && process.env.SECRET_TOKEN !== req.token) {
res.status(401).send("Please use RFC6750 token auth");
return;
}
next();
});
app.get("/:template.png", async (req, res) => {
const { template } = req.params;
try {
await fs.readFile(`templates/${template}.svg`);
} catch (_) {
res.status(400).send(`templates/${template}.svg not found`);
return;
}
const svg = nunjucks.render(`templates/${template}.svg`, req.query); // render template with the context from request GET params
const image = await svgToImg.from(svg).toPng();
res.type("png");
res.send(image);
});
app.use(Sentry.Handlers.errorHandler());
const port = process.env.PORT || 3000;
const host = process.env.HOST || "127.0.0.1";
app.listen(port, host);
console.log(`Server listening on http://${host}:${port}...`);