|
| 1 | +const dotenv = require("dotenv"); |
| 2 | +const fs = require("fs"); |
| 3 | +const path = require("path"); |
| 4 | + |
| 5 | +const logger = require("../libraries/log/logger"); |
| 6 | +const schema = require("./config.schema"); |
| 7 | + |
| 8 | +class Config { |
| 9 | + constructor() { |
| 10 | + if (!Config.instance) { |
| 11 | + logger.info("Loading and validating config for the first time..."); |
| 12 | + this.config = this.loadAndValidateConfig(); |
| 13 | + Config.instance = this; |
| 14 | + logger.info("Config loaded and validated"); |
| 15 | + } |
| 16 | + |
| 17 | + return Config.instance; |
| 18 | + } |
| 19 | + |
| 20 | + loadAndValidateConfig() { |
| 21 | + const environment = process.env.NODE_ENV || "development"; |
| 22 | + |
| 23 | + // 1. Load environment file from one level up and using __dirname |
| 24 | + const envFile = `.env.${environment}`; |
| 25 | + const envPath = path.join(__dirname, "..", envFile); |
| 26 | + if (!fs.existsSync(envPath)) { |
| 27 | + throw new Error(`Environment file not found: ${envPath}`); |
| 28 | + } |
| 29 | + dotenv.config({ path: envPath }); |
| 30 | + |
| 31 | + // 2. Load config file based on environment |
| 32 | + // Construct the path to the config file |
| 33 | + const configFile = path.join(__dirname, `config.${environment}.json`); |
| 34 | + |
| 35 | + // Check if the file exists before trying to read it |
| 36 | + if (!fs.existsSync(configFile)) { |
| 37 | + throw new Error(`Config file not found: ${configFile}`); |
| 38 | + } |
| 39 | + |
| 40 | + let config = JSON.parse(fs.readFileSync(configFile)); |
| 41 | + |
| 42 | + const sharedConfigFile = path.join(__dirname, "config.shared.json"); |
| 43 | + if (fs.existsSync(sharedConfigFile)) { |
| 44 | + const sharedConfig = JSON.parse(fs.readFileSync(sharedConfigFile)); |
| 45 | + config = { ...sharedConfig, ...config }; |
| 46 | + } |
| 47 | + |
| 48 | + const finalConfig = {}; |
| 49 | + for (const key in schema.describe().keys) { |
| 50 | + if (process.env.hasOwnProperty(key)) { |
| 51 | + finalConfig[key] = process.env[key]; // Prioritize environment variables |
| 52 | + } else if (config.hasOwnProperty(key)) { |
| 53 | + finalConfig[key] = config[key]; // Fallback to config file value |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // 4. load the schema file |
| 58 | + if (!schema) { |
| 59 | + throw new Error(`Schema file not found`); |
| 60 | + } |
| 61 | + |
| 62 | + const { error, value: validatedConfig } = schema.validate(finalConfig); |
| 63 | + if (error) { |
| 64 | + const missingProperties = error.details.map((detail) => detail.path[0]); |
| 65 | + throw new Error( |
| 66 | + `Config validation error: missing properties ${missingProperties}`, |
| 67 | + ); |
| 68 | + } |
| 69 | + return validatedConfig; |
| 70 | + } |
| 71 | + |
| 72 | + static getInstance() { |
| 73 | + if (!Config.instance) { |
| 74 | + new Config(); |
| 75 | + } |
| 76 | + return Config.instance; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +module.exports = Config.getInstance().config; |
0 commit comments