mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-18 17:49:31 -07:00
add environment variables for default configuration
This commit is contained in:
33
index.mjs
33
index.mjs
@@ -1,3 +1,4 @@
|
||||
import 'dotenv/config';
|
||||
import express from 'express';
|
||||
import fs from 'fs';
|
||||
import corsPassThru from './cors/index.mjs';
|
||||
@@ -20,7 +21,39 @@ app.get('/playlist.json', playlist);
|
||||
// version
|
||||
const { version } = JSON.parse(fs.readFileSync('package.json'));
|
||||
|
||||
// read and parse environment variables to append to the query string
|
||||
// use the permalink (share) button on the web app to generate a starting point for your configuration
|
||||
// then take each key/value in the querystring and append WSQS_ to the beginning, and then replace any
|
||||
// hyphens with underscores in the key name
|
||||
// environment variables are read from the command line and .env file via the dotenv package
|
||||
|
||||
const qsVars = {};
|
||||
|
||||
Object.entries(process.env).forEach(([key, value]) => {
|
||||
// test for key matching pattern described above
|
||||
if (key.match(/^WSQS_[A-Za-z0-9_]+$/)) {
|
||||
// convert the key to a querystring formatted key
|
||||
const formattedKey = key.replace(/^WSQS_/, '').replaceAll('_', '-');
|
||||
qsVars[formattedKey] = value;
|
||||
}
|
||||
});
|
||||
|
||||
// single flag to determine if environment variables are present
|
||||
const hasQsVars = Object.entries(qsVars).length > 0;
|
||||
|
||||
// turn the environment query string into search params
|
||||
const defaultSearchParams = (new URLSearchParams(qsVars)).toString();
|
||||
|
||||
const index = (req, res) => {
|
||||
// test for no query string in request and if environment query string values were provided
|
||||
if (hasQsVars && Object.keys(req.query).length === 0) {
|
||||
// redirect the user to the query-string appended url
|
||||
const url = new URL(`${req.protocol}://${req.host}${req.url}`);
|
||||
url.search = defaultSearchParams;
|
||||
res.redirect(307, url.toString());
|
||||
return;
|
||||
}
|
||||
// return the standard page
|
||||
res.render('index', {
|
||||
production: false,
|
||||
version,
|
||||
|
||||
Reference in New Issue
Block a user