From c02510659c3909506f9fc13625be14585762843b Mon Sep 17 00:00:00 2001 From: Wingy Date: Tue, 1 Dec 2020 22:09:25 -0500 Subject: [PATCH] env config improvement --- README.md | 37 +++++++++++++++++++++++++++++++++++-- config/index.js | 5 +---- config/wishlist/index.js | 8 +++++++- middlewares/publicRoute.js | 2 +- routes/wishlist/index.js | 8 ++++---- views/wishlist.pug | 6 +++--- 6 files changed, 51 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index eb25109..0a85e0c 100644 --- a/README.md +++ b/README.md @@ -61,9 +61,42 @@ yarn ## Configuration Add environment variables with a .env. Example: -```env -SITE_TITLE="Christmas Zone" +```sh +## Core Settings +# Where to store databases, can be a CouchDB compatible server or directory. +DB_PREFIX=dbs/ +# Where to send someone if they need to log in +DEFAULT_FAILURE_REDIRECT=/login +# Port to listen on PORT=80 +# Expose the internal PouchDB with CouchDB API and Fauxton browser. Mostly used for debugging. Leave empty to disable. +DB_EXPOSE_PORT= +# Proxy to send item data requests to. Leave emtpy to disable. +PROXY_SERVER= +# Secret string to store session cookies with. Automatically generated if not provided. +SECRET= +# How long a user is logged in (milliseconds). Defaults to one week. +SESSION_MAX_AGE=604800000 +# The name of the site in the and navigation bar +SITE_TITLE=Christmas Community +# Used when shared to home screen +SHORT_TITLE=Christmas +# The root path for forms, CSS, and a small amount of JS. Useful when proxying. +ROOT_PATH=/ +# Where to trust the X-Forwarded-For header from. Defaults to "loopback". Useful for proxying to docker. +TRUST_PROXY=loopback + +## Wishlist Settings +# Set to true to not allow users to have their own lists. You may want this for a birthday or wedding. +SINGLE_LIST=false +# Set to false to allow viewing wishlists without logging in +LISTS_PUBLIC=false +# Defaults to true. Set to false for legacy cards view. +TABLE=true +# Convert Amazon links to Amazon Smile links. A percentage of the profit goes to a charity of buyer's choice. Defaults to true. +SMILE=true +# Allow Markdown in item notes. Does not work with TABLE=false. Defaults to false. +MARKDOWN=false ``` ## Startup diff --git a/config/index.js b/config/index.js index fe0c2c3..5f69cd5 100644 --- a/config/index.js +++ b/config/index.js @@ -1,7 +1,5 @@ require('dotenv').config() -const yesNo = require('yes-no') - module.exports = { dbPrefix: process.env.DB_PREFIX || 'dbs/', defaultFailureRedirect: process.env.DEFAULT_FAILURE_REDIRECT || '/login', @@ -14,6 +12,5 @@ module.exports = { shortTitle: process.env.SHORT_TITLE || 'Christmas', wishlist: require('./wishlist'), base: (process.env.ROOT_PATH || '/').endsWith('/') ? (process.env.ROOT_PATH || '/') : `${process.env.ROOT_PATH}/`, - trustProxy: process.env.TRUST_PROXY === 'true' ? true : process.env.TRUST_PROXY || 'loopback', - markdown: yesNo.parse(process.env.MARKDOWN || false) + trustProxy: process.env.TRUST_PROXY === 'true' ? true : process.env.TRUST_PROXY || 'loopback' } diff --git a/config/wishlist/index.js b/config/wishlist/index.js index 88cf57a..eacfff1 100644 --- a/config/wishlist/index.js +++ b/config/wishlist/index.js @@ -1,5 +1,11 @@ +const { parse: yesNo } = require('yes-no') + module.exports = { + singleList: yesNo(process.env.SINGLE_LIST || false), + public: yesNo(process.env.LISTS_PUBLIC || false), + table: yesNo(process.env.TABLE || true), + smile: yesNo(process.env.SMILE || true), note: { - rows: Number(process.env.WISHLIST_NOTE_ROWS) || 5 + markdown: yesNo(process.env.MARKDOWN || false) } } diff --git a/middlewares/publicRoute.js b/middlewares/publicRoute.js index b77b899..59b8943 100644 --- a/middlewares/publicRoute.js +++ b/middlewares/publicRoute.js @@ -1,6 +1,6 @@ const verifyAuth = require('./verifyAuth') -const publicMiddleware = () => process.env.LISTS_PUBLIC === 'true' +const publicMiddleware = () => global._CC.config.wishlist.public ? (req, res, next) => { if (!req.user) req.user = { _id: 'Unknown' } next() diff --git a/routes/wishlist/index.js b/routes/wishlist/index.js index 8b3a735..481c6dc 100644 --- a/routes/wishlist/index.js +++ b/routes/wishlist/index.js @@ -25,7 +25,7 @@ const totals = wishlist => { const ValidURL = (string) => { // Ty SO try { const url = new URL(string) - if (process.env.SMILE !== 'false') { + if (global._CC.config.wishlist.smile) { if (url.hostname === 'www.amazon.com') url.hostname = 'smile.amazon.com' } if (url) return url @@ -39,7 +39,7 @@ module.exports = (db) => { router.get('/', publicRoute(), async (req, res) => { const docs = await db.allDocs({ include_docs: true }) - if (process.env.SINGLE_LIST === 'true') { + if (global._CC.config.wishlist.singleList) { for (const row of docs.rows) { if (row.doc.admin) return res.redirect(`/wishlist/${row.doc._id}`) } @@ -50,7 +50,7 @@ module.exports = (db) => { router.get('/:user', publicRoute(), async (req, res) => { try { const dbUser = await db.get(req.params.user) - if (process.env.SINGLE_LIST === 'true') { + if (global._CC.config.wishlist.singleList) { if (!dbUser.admin) { const docs = await db.allDocs({ include_docs: true }) for (const row of docs.rows) { @@ -63,7 +63,7 @@ module.exports = (db) => { const lastCanSeeValue = wishlistReverse.find(element => (element.addedBy === req.params.user)) const lastCanSee = dbUser.wishlist.indexOf(lastCanSeeValue) for (const item of dbUser.wishlist) { - if (global._CC.config.markdown) item.note = DOMPurify.sanitize(marked(item.note)) + if (global._CC.config.wishlist.note.markdown) item.note = DOMPurify.sanitize(marked(item.note)) } res.render('wishlist', { title: `Wishlist - ${dbUser._id}`, diff --git a/views/wishlist.pug b/views/wishlist.pug index 6c7f743..f375fa6 100644 --- a/views/wishlist.pug +++ b/views/wishlist.pug @@ -13,7 +13,7 @@ block title block content script(type='data/user_id')= req.user._id - if process.env.TABLE !== 'false' + if global._CC.config.wishlist.table .box table.table.has-mobile-cards thead @@ -45,7 +45,7 @@ block content )= (item.name ? item.name : item.url) else td.ugc(data-label='Name')= item.name - if _CC.config.markdown + if _CC.config.wishlist.note.markdown td.ugc(data-label='Note')!= item.note else td.ugc(data-label='Note')= item.note @@ -220,7 +220,7 @@ block print span.is-block Added by: #{item.addedBy} if item.note .box - if _CC.config.markdown + if _CC.config.wishlist.note.markdown span.is-block.ugc!= item.note else span.is-block.ugc= item.note \ No newline at end of file