forked from adrianrudnik/mjml-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (75 loc) · 2.5 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node
'use strict';
var express = require('express'),
bodyParser = require('body-parser'),
os = require('os'),
mjml2html = require('mjml'),
program = require('commander');
program
.usage('[options]')
.parse(process.argv);
var app = express();
app.use(bodyParser.text({
inflate: true,
limit: '2048kb',
type: '*/*'
}));
app.use(require('morgan')('combined'));
var opts = {
keepComments: (process.env.MJML_KEEP_COMMENTS === 'true'),
minify: (process.env.MJML_MINIFY === 'true'),
validationLevel: (['soft', 'strict', 'skip'].includes(process.env.MJML_VALIDATION_LEVEL) ? process.env.MJML_VALIDATION_LEVEL : 'soft')
};
app.all('*', function (req, res) {
// enable cors
if (process.env.CORS) {
res.header("Access-Control-Allow-Origin", process.env.CORS);
res.header("Access-Control-Allow-Headers", "*");
res.header("Access-Control-Allow-Methods", "POST");
res.header("Access-Control-Max-Age", "-1");
}
// ensure content type is set
if (!req.headers['content-type']) {
res.status(500).send('Content-Type must be set, use text/plain if unsure');
return;
}
try {
var mjml = JSON.parse(req.body).mjml
var result = mjml2html(mjml || '', opts);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({ "errors": [], "html" : result.html, "mjml" : mjml}));
} catch (ex) {
// print error details
console.log(req.body || '')
console.error(ex);
console.log('')
res.writeHead(400, {'Content-Type': 'application/json'});
res.end(JSON.stringify({ "errors": ex.message}));
}
});
const server = app.listen(3000);
var signals = {
'SIGHUP': 1,
'SIGINT': 2,
'SIGTERM': 15
};
const shutdown = (signal, value) => {
server.close(() => {
console.log(`app stopped by ${signal} with value ${value}`);
process.exit(128 + value);
});
};
Object.keys(signals).forEach((signal) => {
process.on(signal, () => {
console.log(`process received a ${signal} signal`);
shutdown(signal, signals[signal]);
});
});
console.log('self: ' + os.hostname() + ':80');
console.log('cors: ' + process.env.CORS);
console.log('mjml keep comments: ' + opts.keepComments);
console.log('mjml validation level: ' + opts.validationLevel);
console.log('mjml minify: ' + opts.minify);
console.log('');
console.log('Try to mimic official API (https://mjml.io/api/documentation/)');
console.log('POST JSON, return JSON');