2022-03-15 17:00:15 +00:00
|
|
|
import * as toml from 'toml';
|
|
|
|
import { resolve } from 'path';
|
|
|
|
import { readFile } from 'fs/promises';
|
|
|
|
import { Configuration } from './config.interfaces';
|
2022-08-17 19:48:20 +00:00
|
|
|
import { Provider } from '@nestjs/common';
|
2022-03-15 17:00:15 +00:00
|
|
|
|
|
|
|
const CONFIG_ENV = process.env.NODE_ENV === 'production' ? 'prod' : 'dev';
|
|
|
|
const CONFIG_FILENAME = process.env.CONFIG || `config.${CONFIG_ENV}.toml`;
|
|
|
|
|
2022-08-17 19:48:20 +00:00
|
|
|
export const configProviders: Provider<any>[] = [
|
2022-03-15 17:00:15 +00:00
|
|
|
{
|
|
|
|
provide: 'CONFIG_PATH',
|
|
|
|
useValue: resolve(__dirname, '..', '..', '..', CONFIG_FILENAME),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
provide: 'DEFAULT_CONFIG',
|
|
|
|
useValue: {
|
|
|
|
app: {
|
|
|
|
base_url: 'http://localhost:3000',
|
2022-08-18 07:12:02 +00:00
|
|
|
// generate the following with crypto.randomBytes(256 / 8).toString('hex')
|
2022-03-15 17:00:15 +00:00
|
|
|
session_secret: 'change me!',
|
|
|
|
challenge_secret: 'change me!',
|
2022-08-22 17:39:31 +00:00
|
|
|
registrations: false,
|
2022-03-15 17:00:15 +00:00
|
|
|
},
|
|
|
|
email: {
|
|
|
|
from: 'no-reply@localhost',
|
|
|
|
smtp: {
|
|
|
|
host: 'localhost',
|
|
|
|
port: 587,
|
|
|
|
secure: false,
|
|
|
|
auth: {
|
|
|
|
user: 'root',
|
|
|
|
pass: 'root',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-03-19 10:25:37 +00:00
|
|
|
jwt: {
|
|
|
|
algorithm: 'RS256',
|
|
|
|
issuer: 'localhost',
|
|
|
|
expiration: 3600,
|
|
|
|
},
|
2022-08-18 07:12:02 +00:00
|
|
|
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',
|
|
|
|
},
|
|
|
|
},
|
2022-03-15 17:00:15 +00:00
|
|
|
} as Configuration,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
provide: 'CONFIGURATION',
|
|
|
|
useFactory: async (
|
2022-08-18 07:12:02 +00:00
|
|
|
configPath: string,
|
|
|
|
defaultConfig: Configuration,
|
2022-03-15 17:00:15 +00:00
|
|
|
): Promise<Configuration> => {
|
|
|
|
try {
|
2022-08-18 07:12:02 +00:00
|
|
|
const file = await readFile(configPath, { encoding: 'utf-8' });
|
2022-03-15 17:00:15 +00:00
|
|
|
return {
|
2022-08-18 07:12:02 +00:00
|
|
|
...defaultConfig,
|
2022-08-17 18:56:47 +00:00
|
|
|
...JSON.parse(JSON.stringify(toml.parse(file))),
|
2022-03-15 17:00:15 +00:00
|
|
|
};
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error('Failed to load configuration:', e.message);
|
2022-08-18 07:12:02 +00:00
|
|
|
return defaultConfig;
|
2022-03-15 17:00:15 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
inject: ['CONFIG_PATH', 'DEFAULT_CONFIG'],
|
|
|
|
},
|
|
|
|
];
|