-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapp.module.ts
More file actions
131 lines (128 loc) · 5.4 KB
/
Copy pathapp.module.ts
File metadata and controls
131 lines (128 loc) · 5.4 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
import env from '@kleinkram/backend-common/environment';
import { StorageModule } from '@kleinkram/backend-common/modules/storage/storage.module';
import configuration from '@kleinkram/backend-common/typeorm-config';
import { AccessGroupConfig } from '@kleinkram/shared';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { PassportModule } from '@nestjs/passport';
import { ScheduleModule } from '@nestjs/schedule';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PrometheusModule } from '@willsoto/nestjs-prometheus';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
import { appVersion } from './app-version';
import { AccessModule } from './endpoints/access/access.module';
import { ActionModule } from './endpoints/action/action.module';
import { AuthModule } from './endpoints/auth/auth.module';
import { CategoryModule } from './endpoints/category/category.module';
import { FileModule } from './endpoints/file/file.module';
import { HealthModule } from './endpoints/health/health.module';
import { FoxgloveModule } from './endpoints/integrations/foxglove.module';
import { MissionModule } from './endpoints/mission/mission.module';
import { ProjectModule } from './endpoints/project/project.module';
import { QueueModule } from './endpoints/queue/queue.module';
import { TagModule } from './endpoints/tag/tag.module';
import { TemplatesModule } from './endpoints/templates/templates.module';
import { TopicModule } from './endpoints/topic/topic.module';
import { TriggerModule } from './endpoints/trigger/trigger.module';
import { UserModule } from './endpoints/user/user.module';
import { WorkerModule } from './endpoints/worker/worker.module';
import { APIKeyResolverMiddleware } from './routing/middlewares/api-key-resolver-middleware.service';
import { AuditLoggerMiddleware } from './routing/middlewares/audit-logger-middleware.service';
import { VersionCheckerMiddlewareService } from './routing/middlewares/version-checker-middleware.service';
import { DBDumper } from './services/dbdumper.service';
@Module({
imports: [
PrometheusModule.register({
defaultLabels: {
app: 'backend',
version: appVersion,
},
}),
ConfigModule.forRoot({
envFilePath: ['.env', '../.env'],
isGlobal: true,
load: [
configuration,
(): {
accessConfig: AccessGroupConfig;
} => {
const configPath =
process.env.ACCESS_CONFIG_PATH ??
path.resolve(process.cwd(), '..', 'access_config.json');
const accessConfig = JSON.parse(
fs.readFileSync(configPath, 'utf8'),
) as AccessGroupConfig;
return { accessConfig };
},
],
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) =>
({
type: 'postgres',
host: configService.getOrThrow<string>('database.host'),
port: configService.getOrThrow<number>('database.port'),
username:
configService.getOrThrow<string>('database.username'),
password:
configService.getOrThrow<string>('database.password'),
database:
configService.getOrThrow<string>('database.database'),
entities: configService.getOrThrow('entities'),
synchronize: env.DEV,
logging: ['warn', 'error'],
}) as PostgresConnectionOptions,
inject: [ConfigService],
}),
FileModule,
HealthModule,
FoxgloveModule,
ProjectModule,
TopicModule,
MissionModule,
QueueModule,
TriggerModule,
UserModule,
AuthModule,
AccessModule,
PassportModule,
ActionModule,
TemplatesModule,
TagModule,
WorkerModule,
CategoryModule,
ScheduleModule.forRoot(),
StorageModule,
],
providers: [DBDumper],
})
export class AppModule implements NestModule {
/**
* Apply middleware to all routes.
* Middleware is a function which is called before the route handler.
*
* The middleware function has access to the request and response objects
* and is used find the user by the API key or JWT token.
*
* @param consumer
*/
configure(consumer: MiddlewareConsumer): void {
consumer
.apply(APIKeyResolverMiddleware, AuditLoggerMiddleware)
.forRoutes('*');
consumer
.apply(VersionCheckerMiddlewareService)
.exclude(
'/auth/{*path}', // excludes auth endpoints
'/integrations/{*path}', // excludes integration endpoints
'/hooks/{*path}', // excludes hook endpoints
'/api/health', // excludes health check
'/', // excludes root for convenience
'/favicon.ico', // excludes favicon for browser requests
)
.forRoutes('*');
}
}