-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.mjs
55 lines (49 loc) · 1.55 KB
/
index.mjs
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
import convertSvgToPng from "convert-svg-to-png";
import express from "express";
import bodyParser from "body-parser";
const serverPort = 3000;
const app = express();
const converter = convertSvgToPng.createConverter({
puppeteer: {
args: ['--no-sandbox', '--disable-setuid-sandbox']
}
});
app.use(bodyParser.text({
type: '*/*',
limit: '10mb',
}));
app.post('/convert', async (req, res) => {
try {
const options = {};
options.background = req.query.background || '#fff';
options.baseUrl = req.query.baseUrl || 'file:///app';
options.scale = req.query.scale || 1;
if (req.query.baseFile) options.baseFile = req.query.baseFile;
if (req.query.height) options.height = req.query.height;
if (req.query.width) options.width = req.query.width;
console.log('convert svg', options);
const png = await converter.convert(req.body, options);
res.set('Content-Type', 'image/png');
res.send(png);
} catch (e) {
res.status(500).send(e.message);
console.log('error', e);
}
});
const server = app.listen(serverPort);
// HTTP Keep-Alive to a short time to allow graceful shutdown
server.on('connection', function (socket) {
socket.setTimeout(5 * 1000);
});
const shutdown = async () => {
console.log('graceful shutdown puppeteer');
await converter.destroy();
console.log('graceful shutdown express');
server.close(function () {
console.log('closed express');
process.exit();
});
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
console.log('Running server: http://0.0.0.0:' + serverPort);