-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: env variable with docker secret
- Loading branch information
1 parent
a9101c7
commit 3e160c7
Showing
1 changed file
with
30 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} | ||
} | ||
} | ||
}; |