From 982514c7275572744e509803b7f88cc660cc5235 Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Mon, 18 Nov 2024 18:55:34 +0300 Subject: [PATCH] build: check all relevant environment variables are set on start up --- public/index.html | 2 +- server.js | 3 +++ src/envValidator.js | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/envValidator.js diff --git a/public/index.html b/public/index.html index 2fef9c2..67f2353 100644 --- a/public/index.html +++ b/public/index.html @@ -57,7 +57,7 @@

Get Started

Ready to take the next step in your IT career? Apply today!

-
+ diff --git a/server.js b/server.js index 5a1bf28..3bb91d3 100644 --- a/server.js +++ b/server.js @@ -2,9 +2,12 @@ import express from 'express'; import dotenv from 'dotenv'; import path from 'path'; // Import the path module import routes from './src/routes.js'; // Import routes from routes.js +import { validateEnvVariables } from './src/envValidator.js'; dotenv.config(); +validateEnvVariables(); + const app = express(); const PORT = process.env.PORT || 3002; diff --git a/src/envValidator.js b/src/envValidator.js new file mode 100644 index 0000000..e5a38d3 --- /dev/null +++ b/src/envValidator.js @@ -0,0 +1,16 @@ +export function validateEnvVariables() { + const requiredVars = [ + 'LISTMONK_URL', + 'LISTMONK_USERNAME', + 'LISTMONK_PASSWORD', + 'LIST_ID' + ]; + + const missingVars = requiredVars.filter((variable) => !process.env[variable]); + + if (missingVars.length > 0) { + console.error(`Error: Missing environment variables: ${missingVars.join(', ')}`); + process.exit(1); // Exit the application with a failure code + } + console.log('All required environment variables are set.'); +}