env config improvement

This commit is contained in:
Wingy 2020-12-01 22:09:25 -05:00
parent f3cfc6abe0
commit c02510659c
6 changed files with 51 additions and 15 deletions

View file

@ -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 <title> 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

View file

@ -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'
}

View file

@ -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)
}
}

View file

@ -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()

View file

@ -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}`,

View file

@ -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