add environment variables for default configuration

This commit is contained in:
Matt Walsh
2025-05-13 23:04:46 -05:00
parent 262ea15468
commit e952d860dc
6 changed files with 57 additions and 5 deletions

3
.gitignore vendored
View File

@@ -12,3 +12,6 @@ server/music/*
#dist folder #dist folder
dist/* dist/*
!dist/readme.txt !dist/readme.txt
#environment variables
.env

View File

@@ -92,11 +92,12 @@ Kiosk mode can be activated by a checkbox on the page. Note that there is no way
It's also possible to enter kiosk mode using a permalink. First generate a [Permalink](#sharing-a-permalink-bookmarking), then to the end of it add `&kiosk=true`. Opening this link will load all of the selected displays included in the Permalink, enter kiosk mode immediately upon loading and start playing the forecast. It's also possible to enter kiosk mode using a permalink. First generate a [Permalink](#sharing-a-permalink-bookmarking), then to the end of it add `&kiosk=true`. Opening this link will load all of the selected displays included in the Permalink, enter kiosk mode immediately upon loading and start playing the forecast.
## Wish list ## Default query string paramaters (environment variables)
When serving this via the built-in Express server, it's possible to define environment variables that direct the user to a default set of paramaters (like the [Permalink](#sharing-a-permalink-bookmarking) above). If a user requests the root page at `http://localhost:8080/` the query string provided by environment variables will be appended to the url thus providing a default configuration.
As time allows I will be working on the following enhancements. Environment variables can be added to the command line as usual, or via a .env file which is parsed with [dotenv](https://github.com/motdotla/dotenv). Both methods have the same effect.
* Better error reporting when api.weather.gov is down (happens more often than you would think) Environment variables that are to be added to the default query string are prefixed with `WSQS_` and then use the same key/value pairs generated by the [Permalink](#sharing-a-permalink-bookmarking) above, with the `- (dash)` character replaced by an `_ (underscore)`. For example, if you wanted to turn the travel forecast on, you would find `travel-checkbox=true` in the permalink, it's matching environment variable becomes `WSQS_travel_checkbox=true`.
## Serving static files ## Serving static files
The app can be served as a static set of files on any web server. Run the provided gulp task to create a set of static distribution files: The app can be served as a static set of files on any web server. Run the provided gulp task to create a set of static distribution files:

View File

@@ -1,3 +1,4 @@
import 'dotenv/config';
import express from 'express'; import express from 'express';
import fs from 'fs'; import fs from 'fs';
import corsPassThru from './cors/index.mjs'; import corsPassThru from './cors/index.mjs';
@@ -20,7 +21,39 @@ app.get('/playlist.json', playlist);
// version // version
const { version } = JSON.parse(fs.readFileSync('package.json')); 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) => { 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', { res.render('index', {
production: false, production: false,
version, version,

13
package-lock.json generated
View File

@@ -9,6 +9,7 @@
"version": "5.17.0", "version": "5.17.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"dotenv": "^16.5.0",
"ejs": "^3.1.5", "ejs": "^3.1.5",
"express": "^5.1.0" "express": "^5.1.0"
}, },
@@ -3813,6 +3814,18 @@
"node": ">=6.0.0" "node": ">=6.0.0"
} }
}, },
"node_modules/dotenv": {
"version": "16.5.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.5.0.tgz",
"integrity": "sha512-m/C+AwOAr9/W1UOIZUo232ejMNnJAJtYQjUbHoNTBNTJSvqzzDh7vnrei3o3r3m9blf6ZoDkvcw0VmozNRFJxg==",
"license": "BSD-2-Clause",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://dotenvx.com"
}
},
"node_modules/dunder-proto": { "node_modules/dunder-proto": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",

View File

@@ -46,6 +46,7 @@
"webpack-stream": "^7.0.0" "webpack-stream": "^7.0.0"
}, },
"dependencies": { "dependencies": {
"dotenv": "^16.5.0",
"ejs": "^3.1.5", "ejs": "^3.1.5",
"express": "^5.1.0" "express": "^5.1.0"
} }

View File

@@ -33,7 +33,8 @@
"T'storm", "T'storm",
"uscomp", "uscomp",
"Visib", "Visib",
"Waukegan" "Waukegan",
"WSQS"
], ],
"cSpell.ignorePaths": [ "cSpell.ignorePaths": [
"**/package-lock.json", "**/package-lock.json",