diff options
author | Mark Powers <mark@marks.kitchen> | 2022-07-30 16:55:06 -0500 |
---|---|---|
committer | Mark Powers <mark@marks.kitchen> | 2022-07-30 16:55:06 -0500 |
commit | eadb979910da546fd64b937a0d71d2a6c4f3f934 (patch) | |
tree | 7e72c26b8625b0202dc833a36d2fa8b04d0b56bb /src/server.js |
Diffstat (limited to 'src/server.js')
-rw-r--r-- | src/server.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/server.js b/src/server.js new file mode 100644 index 0000000..7192f49 --- /dev/null +++ b/src/server.js @@ -0,0 +1,51 @@ +const express = require('express'); +const bodyParser = require('body-parser'); +const crypto = require('crypto'); +const cache = require('apicache').middleware; + +const server = express(); +server.use(bodyParser.urlencoded({ extended: true })); + +function listen(port) { + server.listen(port, () => console.info(`Listening on port ${port}!`)); +} + +function setUpRoutes(models, jwtFunctions, database) { + server.get('/', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/email.html")) + server.get('/success', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/email-success.html")) + server.get('/unsubscribe', cache('5 minutes'), (req, res) => res.sendFile(__dirname + "/html/email-unsubscribe.html")) + server.post('/', async (req, res, next) => { + const name = req.body.name; + const email = req.body.email; + if (name && email) { + const code = crypto.randomBytes(40).toString('hex').slice(0, 40) + models.emails.create({"name": name, "address": email, "code": code}) + res.redirect('/success'); + } else { + console.debug("Error with email submission") + } + }) + server.get('/unsubscribe/:code/check', async (req, res, next) => { + res.sendFile(__dirname + "/html/email-confirm.html") + }) + server.get('/unsubscribe/:code/confirm', async (req, res, next) => { + await models.emails.destroy({ where: {"code": req.params.code}}) + res.redirect('/unsubscribe'); + }) + server.get('/css/:id', cache('5 minutes'), (req, res) => { + res.sendFile(__dirname + "/css/" + req.params.id); + }); + + + // Final 404 fallback + server.use(function(req, res) { + res.status(400); + res.sendFile(__dirname + "/html/404.html"); + }); +} + +module.exports = { + listen, + setUpRoutes +}; + |