forked from ToolJet/ToolJet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.ts
More file actions
136 lines (129 loc) · 5.44 KB
/
loader.ts
File metadata and controls
136 lines (129 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { DynamicModule } from '@nestjs/common';
import { getImportPath } from './constants';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { ScheduleModule } from '@nestjs/schedule';
import { BullModule } from '@nestjs/bull';
import { ConfigModule } from '@nestjs/config';
import { getEnvVars } from '../../../scripts/database-config-utils';
import { LoggerModule } from 'nestjs-pino';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ormconfig, tooljetDbOrmconfig } from '../../../ormconfig';
import { RequestContextModule } from '@modules/request-context/module';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { GuardValidatorModule } from './validators/feature-guard.validator';
import { SentryModule } from '@modules/observability/sentry/module';
export class AppModuleLoader {
static async loadModules(configs: {
IS_GET_CONTEXT: boolean;
}): Promise<(DynamicModule | typeof GuardValidatorModule)[]> {
// Static imports that are always loaded
const staticModules = [
EventEmitterModule.forRoot({
wildcard: false,
newListener: false,
removeListener: false,
maxListeners: 5,
verboseMemoryLeak: true,
ignoreErrors: false,
}),
ScheduleModule.forRoot(),
BullModule.forRoot({
redis: {
host: process.env.REDIS_HOST || 'localhost',
port: parseInt(process.env.REDIS_PORT) || 6379,
},
}),
await ConfigModule.forRoot({
isGlobal: true,
envFilePath: [`../.env.${process.env.NODE_ENV}`, '../.env'],
load: [() => getEnvVars()],
}),
LoggerModule.forRoot({
pinoHttp: {
level: (() => {
const logLevel = {
production: 'info',
development: 'debug',
test: 'error',
};
return logLevel[process.env.NODE_ENV] || 'info';
})(),
autoLogging: {
ignorePaths: ['/api/health'],
},
prettyPrint:
process.env.NODE_ENV !== 'production'
? {
colorize: true,
levelFirst: true,
translateTime: 'UTC:mm/dd/yyyy, h:MM:ss TT Z',
}
: false,
redact: {
paths: [
'req.headers.authorization',
'req.headers.cookie',
'res.headers.authorization',
'res.headers["set-cookie"]',
'req.headers["proxy-authorization"]',
'req.headers["www-authenticate"]',
'req.headers["authentication-info"]',
'req.headers["x-forwarded-for"]',
...(process.env.LOGGER_REDACT ? process.env.LOGGER_REDACT?.split(',') : []),
],
censor: '[REDACTED]',
},
},
}),
TypeOrmModule.forRoot(ormconfig),
TypeOrmModule.forRoot(tooljetDbOrmconfig),
RequestContextModule,
GuardValidatorModule,
];
if (process.env.SERVE_CLIENT !== 'false' && process.env.NODE_ENV === 'production') {
staticModules.unshift(
ServeStaticModule.forRoot({
// Have to remove trailing slash of SUB_PATH.
serveRoot: process.env.SUB_PATH === undefined ? '' : process.env.SUB_PATH.replace(/\/$/, ''),
rootPath: join(__dirname, '../../../../../', 'frontend/build'),
})
);
}
if (process.env.APM_VENDOR == 'sentry') {
staticModules.unshift(
SentryModule.forRoot({
dsn: process.env.SENTRY_DNS,
tracesSampleRate: 1.0,
debug: !!process.env.SENTRY_DEBUG,
})
);
}
/**
* ███████████████████████████████████████████████████████████████████████████████
* █ █
* █ DYNAMIC MODULES █
* █ █
* █ Modules added here can be easily enabled with minimal code changes. █
* █ Not keeping the code on base directories. █
* █ █
* ███████████████████████████████████████████████████████████████████████████████
*/
const dynamicModules: DynamicModule[] = [];
if (!configs.IS_GET_CONTEXT) {
// Load dynamic modules only when not in migration context
try {
if (process.env.LOG_FILE_PATH) {
// Add log-to-file module if LOG_FILE_PATH is set
const { LogToFileModule } = await import(`${await getImportPath(configs.IS_GET_CONTEXT)}/log-to-file/module`);
dynamicModules.push(await LogToFileModule.register(configs));
}
const { AuditLogsModule } = await import(`${await getImportPath(configs.IS_GET_CONTEXT)}/audit-logs/module`);
dynamicModules.push(await AuditLogsModule.register(configs));
} catch (error) {
console.error('Error loading dynamic modules:', error);
}
}
return [...staticModules, ...dynamicModules];
}
}