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', // generate the following with crypto.randomBytes(256 / 8).toString('hex') session_secret: 'change me!', challenge_secret: 'change me!', registrations: false, }, email: { from: 'no-reply@localhost', smtp: { host: 'localhost', port: 587, secure: false, auth: { user: 'root', pass: 'root', }, }, }, jwt: { algorithm: 'RS256', issuer: 'localhost', expiration: 3600, }, database: { type: 'mysql', host: 'localhost', port: 3306, username: 'root', password: 'root', database: 'icyauth', entities: [__dirname + '/../**/*.entity{.ts,.js}'], synchronize: true, migrations: ['migration/*.js'], cli: { migrationsDir: 'migration', }, }, } as Configuration, }, { provide: 'CONFIGURATION', useFactory: async ( configPath: string, defaultConfig: Configuration, ): Promise => { try { const file = await readFile(configPath, { encoding: 'utf-8' }); return { ...defaultConfig, ...JSON.parse(JSON.stringify(toml.parse(file))), }; } catch (e: any) { console.error('Failed to load configuration:', e.message); return defaultConfig; } }, inject: ['CONFIG_PATH', 'DEFAULT_CONFIG'], }, ];