Skip to content

Commit

Permalink
added MJML_KEEP_COMMENTS, MJML_VALIDATION_LEVEL and MJML_MINIFY env v…
Browse files Browse the repository at this point in the history
…ariable settings. Fixes #1, fixes #2, fixes #3, fixes #4, fixes #5.
  • Loading branch information
adrianrudnik committed Jan 27, 2019
1 parent b50db16 commit 4da61bb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 20 deletions.
10 changes: 8 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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 \
Expand All @@ -11,8 +19,6 @@ COPY index.js ./index.js

COPY healthcheck.sh /healthcheck.sh

ENV CORS=""

HEALTHCHECK --timeout=2s \
CMD /healthcheck.sh || exit 1

Expand Down
38 changes: 33 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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.
Expand Down
37 changes: 24 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <origin>', '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");
Expand All @@ -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();
}
});

Expand All @@ -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.');

0 comments on commit 4da61bb

Please sign in to comment.