import * as toml from 'toml'; import { resolve } from 'path'; import { readFile } from 'fs/promises'; import { Configuration } from './config.interfaces'; import { Provider } from '@nestjs/common'; const CONFIG_ENV = process.env.NODE_ENV === 'production' ? 'prod' : 'dev'; const CONFIG_FILENAME = process.env.CONFIG || `config.${CONFIG_ENV}.toml`; export const configProviders: Provider[] = [ { provide: 'CONFIG_PATH', useValue: resolve(__dirname, '..', '..', '..', CONFIG_FILENAME), }, { provide: 'DEFAULT_CONFIG', useValue: { app: { base_url: 'http://localhost:3000', session_secret: 'change me!', challenge_secret: 'change me!', }, email: { from: 'no-reply@localhost', smtp: { host: 'localhost', port: 587, secure: false, auth: { user: 'root', pass: 'root', }, }, }, jwt: { algorithm: 'RS256', issuer: 'localhost', expiration: 3600, }, } as Configuration, }, { provide: 'CONFIGURATION', useFactory: async ( path: string, def: Configuration, ): Promise => { try { const file = await readFile(path, { encoding: 'utf-8' }); return { ...def, ...JSON.parse(JSON.stringify(toml.parse(file))), }; } catch (e: any) { console.error('Failed to load configuration:', e.message); return def; } }, inject: ['CONFIG_PATH', 'DEFAULT_CONFIG'], }, ];