From 4da61bbd0cd84cdf71b9f53ff382c8a190192f5c Mon Sep 17 00:00:00 2001 From: Adrian Rudnik Date: Sun, 27 Jan 2019 15:17:56 +0100 Subject: [PATCH] added MJML_KEEP_COMMENTS, MJML_VALIDATION_LEVEL and MJML_MINIFY env variable settings. Fixes #1, fixes #2, fixes #3, fixes #4, fixes #5. --- Dockerfile | 10 ++++++++-- README.md | 38 +++++++++++++++++++++++++++++++++----- index.js | 37 ++++++++++++++++++++++++------------- 3 files changed, 65 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index 60e8930..97c5ed8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,13 @@ FROM node:alpine +ENV NODE_ENV=production + +ENV CORS="" + +ENV MJML_KEEP_COMMENTS=false +ENV MJML_VALIDATION_LEVEL=soft +ENV MJML_MINIFY=true + COPY package* ./ RUN set -ex \ @@ -11,8 +19,6 @@ COPY index.js ./index.js COPY healthcheck.sh /healthcheck.sh -ENV CORS="" - HEALTHCHECK --timeout=2s \ CMD /healthcheck.sh || exit 1 diff --git a/README.md b/README.md index e08fea3..356d253 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,10 @@ Due to various challenges this image sports the following features: - Supports healthchecks. # Table of contents -- [MJML docker microservice / server](#mjml-docker-microservice--server) -- [Table of contents](#table-of-contents) - - [Overview](#overview) - - [Troubleshooting](#troubleshooting) +- [Overview](#overview) +- [Defaults](#defaults) +- [Development](#development) +- [Troubleshooting](#troubleshooting) ## Overview @@ -24,7 +24,7 @@ Due to GDPR / DSGVO reasons I required the mjml instance to be under my own cont Starting the image is as easy as running a test instance through docker ```sh -docker run -it --rm -p 8889:80 mjml-server # --cors "*" +docker run -it --rm -p 8889:80 mjml-server ``` or `docker-compose` with the following example: @@ -37,10 +37,38 @@ services: image: adrianrudnik/mjml-server ports: - 8889:80 + # for development, uncomment the following lines: # environment: # CORS: * + # MJML_KEEP_COMMENTS=true + # MJML_VALIDATION_LEVEL=strict + # MJML_MINIFY=false ``` +## Defaults + +The production defaults, without any override, default to: + +```sh +CORS "" +MJML_KEEP_COMMENTS "false" +MJML_VALIDATION_LEVEL "soft" +MJML_MINIFY "true" +``` + +## Development + +For development environments I would suggest to switch it to + +```sh +CORS "*" +MJML_KEEP_COMMENTS "true" +MJML_VALIDATION_LEVEL "strict" +MJML_MINIFY "false" +``` + +This will escalate any issues you have with invalid mjml code to the docker log (`stdout` or `docker-compose logs`). + ## Troubleshooting Make sure you pass along a plain Content-Type header and pass the mjml as raw body. diff --git a/index.js b/index.js index 1e95077..a7a1417 100644 --- a/index.js +++ b/index.js @@ -8,25 +8,28 @@ var express = require('express'), mjml2html = require('mjml'), program = require('commander'); -program.version('1.0.0') +program .usage('[options]') - .option('-c, --cors ', 'enable cors header with given origin rule', process.env.CORS) .parse(process.argv); var app = express(); -var opts = { +app.use(bodyParser.text({ inflate: true, limit: '2048kb', type: '*/*' -}; +})); -app.use(bodyParser.text(opts)); +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 (program.cors) { - res.header("Access-Control-Allow-Origin", program.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"); @@ -37,15 +40,19 @@ app.all('*', function (req, res) { res.status(500).send('Content-Type must be set, use text/plain if unsure'); return; } - + try { - var result = mjml2html(req.body || ''); + var result = mjml2html(req.body || '', opts); res.writeHead(200, {'Content-Type': 'text/html'}); res.end(result.html); } catch (ex) { + // print error details + console.log(req.body || '') console.error(ex); + console.log('') + res.writeHead(400, {'Content-Type': 'text/plain'}); - res.end(ex); + res.end(); } }); @@ -71,6 +78,10 @@ Object.keys(signals).forEach((signal) => { }); }); -console.log('self: ' + os.hostname() + ':' + program.port) -console.log('cors: ' + program.cors) -console.log('POST mjml as text/plain raw body, result will be returned as text/plain.'); +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('POST mjml as text/plain raw body, result will be returned as text/html.');