-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (71 loc) · 2.05 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
"use strict";
const requireStaticAssets = (module, filePath) => {
module.exports = readFileSync(filePath, "utf8")
}
require.extensions[".css"] = requireStaticAssets
require.extensions[".html"] = requireStaticAssets
// Modules declarations
const ejs = require("ejs")
const path = require("path")
const juice = require("juice")
const cheerio = require("cheerio")
const express = require("express")
const mustache = require("mustache")
const { readFileSync } = require("fs")
const bodyParser = require('body-parser')
const { minify } = require("html-minifier")
const { unescape } = require("html-escaper")
const { celebrate, Joi, errors } = require("celebrate")
const organization = require("./organization")
const loadRawTemplate = (filename) => {
let resolvedFilepath = path.resolve(process.cwd(), "templates", filename)
return readFileSync(resolvedFilepath, "utf8")
}
const app = express()
const tachyons = require("tachyons/css/tachyons.css")
const layout = loadRawTemplate("layout.html")
const loadRenderer = (templateFilename) => {
let template = loadRawTemplate(templateFilename)
let withinLayout = mustache.render(layout, {
content: template,
css: tachyons,
organization
})
let inlinedCSS = juice(withinLayout)
const $ = cheerio.load(inlinedCSS)
$("style").remove()
$("*").removeAttr("class")
let unescaped = unescape($.html())
let minified = minify(unescaped, {
collapseWhitespace: true,
conservativeCollapse: true,
minifyCSS: true,
removeComments: true,
})
let ejsFunc = ejs.compile(minified)
return ejsFunc
}
const routes = [
{
path: "/verify-email-ownership",
validationRule: {
body: Joi.object().keys({
validationLink: Joi.string().required()
})
},
renderer: loadRenderer("verify-email-ownership.html")
}
]
app.use(bodyParser.json())
app.use(errors())
routes.forEach(route => {
app.post(route.path,
celebrate(route.validationRule),
(req, res, next) => {
let rendered = route.renderer(req.body)
res.send(rendered)
next()
}
)
})
module.exports = app