Skip to content

Commit

Permalink
fix: env variable with docker secret
Browse files Browse the repository at this point in the history
  • Loading branch information
David200308 committed Dec 30, 2024
1 parent a9101c7 commit 3e160c7
Showing 1 changed file with 30 additions and 95 deletions.
125 changes: 30 additions & 95 deletions Backend/src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,103 +1,38 @@
import * as fs from 'fs';
import 'dotenv/config';

export const checkEnv = () => {
const requiredEnvVars = [
'DB_HOST',
'DB_USER',
'DB_PASS',
'DB_NAME',
'JWT_PRIVATE_KEY',
'JWT_PUBLIC_KEY',
'DOCS_USER',
'DOCS_PASSWORD',
'PASSKEY_RPNAME',
'PASSKEY_RPID',
'PASSKEY_ORIGIN',
'AES_KEY',
'MAILGUN_API_KEY',
'MAILGUN_FROM_DOMAIN',
'SENTRY_DSN'
{ varName: 'DB_HOST' },
{ varName: 'DB_USER' },
{ varName: 'DB_PASS', isFile: true },
{ varName: 'DB_NAME' },
{ varName: 'JWT_PRIVATE_KEY', isFile: true },
{ varName: 'JWT_PUBLIC_KEY', isFile: true },
{ varName: 'DOCS_USER', isFile: true },
{ varName: 'DOCS_PASSWORD', isFile: true },
{ varName: 'PASSKEY_RPNAME', isFile: true },
{ varName: 'PASSKEY_RPID', isFile: true },
{ varName: 'PASSKEY_ORIGIN', isFile: true },
{ varName: 'AES_KEY', isFile: true },
{ varName: 'MAILGUN_API_KEY', isFile: true },
{ varName: 'MAILGUN_FROM_DOMAIN', isFile: true },
{ varName: 'SENTRY_DSN', isFile: true },
];

for (const varName of requiredEnvVars) {
switch (varName) {
case 'DB_HOST':
if (!process.env.DB_HOST) {
throw new Error('Please provide DB_HOST in .env file');
}
break;
case 'DB_USER':
if (!process.env.DB_USER) {
throw new Error('Please provide DB_USER in .env file');
}
break;
case 'DB_PASS':
if (!process.env.DB_PASS) {
throw new Error('Please provide DB_PASS in .env file');
}
break;
case 'DB_NAME':
if (!process.env.DB_NAME) {
throw new Error('Please provide DB_NAME in .env file');
}
break;
case 'JWT_PRIVATE_KEY':
if (!process.env.JWT_PRIVATE_KEY) {
throw new Error('Please provide JWT_PRIVATE_KEY in .env file');
}
break;
case 'JWT_PUBLIC_KEY':
if (!process.env.JWT_PUBLIC_KEY) {
throw new Error('Please provide JWT_PUBLIC_KEY in .env file');
}
break;
case 'DOCS_USER':
if (!process.env.DOCS_USER) {
throw new Error('Please provide DOCS_USER in .env file');
}
break;
case 'DOCS_PASSWORD':
if (!process.env.DOCS_PASSWORD) {
throw new Error('Please provide DOCS_PASSWORD in .env file');
}
break;
case 'PASSKEY_RPNAME':
if (!process.env.PASSKEY_RPNAME) {
throw new Error('Please provide PASSKEY_RPNAME in .env file');
}
break;
case 'PASSKEY_RPID':
if (!process.env.PASSKEY_RPID) {
throw new Error('Please provide PASSKEY_RPID in .env file');
}
break;
case 'PASSKEY_ORIGIN':
if (!process.env.PASSKEY_ORIGIN) {
throw new Error('Please provide PASSKEY_ORIGIN in .env file');
}
break;
case 'AES_KEY':
if (!process.env.AES_KEY) {
throw new Error('Please provide AES_KEY in .env file');
}
break;
case 'MAILGUN_API_KEY':
if (!process.env.MAILGUN_API_KEY) {
throw new Error('Please provide MAILGUN_API_KEY in .env file');
}
break;
case 'MAILGUN_FROM_DOMAIN':
if (!process.env.MAILGUN_FROM_DOMAIN) {
throw new Error('Please provide MAILGUN_FROM_DOMAIN in .env file');
}
break;
case 'SENTRY_DSN':
if (!process.env.SENTRY_DSN) {
throw new Error('Please provide SENTRY_DSN in .env file');

for (const { varName, isFile } of requiredEnvVars) {
if (isFile) {
const filePath = process.env[`${varName}_FILE`];
if (!filePath || !fs.existsSync(filePath)) {
throw new Error(`Missing or invalid file path for ${varName}_FILE`);
}
const value = fs.readFileSync(filePath, 'utf-8').trim();
if (!value) {
throw new Error(`File ${filePath} for ${varName}_FILE is empty`);
}
break;
default:
continue;
process.env[varName] = value;
} else if (!process.env[varName]) {
throw new Error(`Please provide ${varName} in the environment`);
}
}
}
};

0 comments on commit 3e160c7

Please sign in to comment.