58 lines
1.5 KiB
JavaScript
58 lines
1.5 KiB
JavaScript
// config.js - Configuration handling
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import dotenv from 'dotenv';
|
|
|
|
// Load environment variables from .env file
|
|
dotenv.config();
|
|
|
|
// Configure paths relative to this file
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
const CONFIG_PATH = path.resolve(__dirname, '../config.json');
|
|
|
|
// Determine environment
|
|
const NODE_ENV = process.env.NODE_ENV || 'development';
|
|
|
|
// Load config file
|
|
export function loadConfig() {
|
|
try {
|
|
if (!fs.existsSync(CONFIG_PATH)) {
|
|
throw new Error(`Configuration file not found at ${CONFIG_PATH}`);
|
|
}
|
|
|
|
const configData = fs.readFileSync(CONFIG_PATH, 'utf8');
|
|
const config = JSON.parse(configData);
|
|
|
|
if (!config[NODE_ENV]) {
|
|
throw new Error(
|
|
`Configuration for environment '${NODE_ENV}' not found in config.json`
|
|
);
|
|
}
|
|
|
|
const envConfig = config[NODE_ENV];
|
|
|
|
// Override keycloak client secret with environment variable if available
|
|
if (process.env.KEYCLOAK_CLIENT_SECRET) {
|
|
if (!envConfig.auth) {
|
|
envConfig.auth = {};
|
|
}
|
|
if (!envConfig.auth.keycloak) {
|
|
envConfig.auth.keycloak = {};
|
|
}
|
|
envConfig.auth.keycloak.clientSecret = process.env.KEYCLOAK_CLIENT_SECRET;
|
|
}
|
|
|
|
return envConfig;
|
|
} catch (err) {
|
|
console.error('Error loading config:', err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
// Get current environment
|
|
export function getEnvironment() {
|
|
return NODE_ENV;
|
|
}
|