@seas-computing/nestjs-healthcheck 0.0.13-0 → 0.0.13-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -60
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# NestJS HealthCheck Module
|
|
2
2
|
|
|
3
|
-
The `nestjs-healthcheck` package provides a
|
|
3
|
+
The `nestjs-healthcheck` package provides a standalone NestJS module that verifies the availability of essential application dependencies. This includes:
|
|
4
4
|
- pinging the application database using `TypeOrmHealthIndicator`
|
|
5
|
-
- performing an HTTP ping to the HarvardKey
|
|
6
|
-
- pinging
|
|
7
|
-
It returns a composite health status result and the current app version. It is intended to be included in a larger application and assumes the presence of a
|
|
5
|
+
- performing an HTTP ping to the HarvardKey OIDC discovery document URL using `HttpHealthIndicator`
|
|
6
|
+
- pinging Valkey with a custom `ValkeyHealthService`
|
|
7
|
+
It returns a composite health status result and the current app version. It is intended to be included in a larger application and assumes the presence of a Valkey connection and database connection during runtime.
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
10
10
|
```
|
|
@@ -16,15 +16,16 @@ npm install @seas-computing/nestjs-healthcheck
|
|
|
16
16
|
To add the nestjs healthcheck system, you should make the following changes in the consuming application's `AppModule` (or other root module):
|
|
17
17
|
- be sure that the `ConfigModule` is at the top of the imports list
|
|
18
18
|
- import and register the `HealthcheckModule` using `registerAsync`
|
|
19
|
-
- import `TypeOrmModule` and `
|
|
19
|
+
- import `TypeOrmModule` and `ValkeyModule`
|
|
20
20
|
|
|
21
21
|
In the `ConfigService`, configuration values (i.e. `HealthcheckModuleOptions`) should be made available and passed as options to the `HealthcheckModule`.
|
|
22
22
|
|
|
23
|
-
Example `app.module` from [
|
|
23
|
+
Example `app.module` from [oap-grad-database](https://github.huit.harvard.edu/SEAS/oap-grad-database/blob/develop/src/server/app.module.ts):
|
|
24
24
|
```ts
|
|
25
25
|
@Module({
|
|
26
26
|
imports: [
|
|
27
|
-
ConfigModule,
|
|
27
|
+
ConfigModule, // Ensure this is at the top
|
|
28
|
+
HealthCheckModule,
|
|
28
29
|
LogModule.registerAsync({
|
|
29
30
|
imports: [ConfigModule],
|
|
30
31
|
inject: [ConfigService],
|
|
@@ -35,24 +36,22 @@ Example `app.module` from [makerspace](https://github.huit.harvard.edu/SEAS/make
|
|
|
35
36
|
imports: [ConfigModule],
|
|
36
37
|
useFactory: (
|
|
37
38
|
config: ConfigService
|
|
38
|
-
)
|
|
39
|
+
) => ({
|
|
40
|
+
...config.harvardKeyConfig,
|
|
41
|
+
}),
|
|
39
42
|
}),
|
|
40
43
|
SessionModule.forRootAsync({
|
|
41
44
|
imports: [ConfigModule],
|
|
42
45
|
inject: [ConfigService],
|
|
43
|
-
useFactory:
|
|
46
|
+
useFactory: (
|
|
44
47
|
config: ConfigService
|
|
45
|
-
):
|
|
46
|
-
const client =
|
|
47
|
-
// Version 4 of redis does not automatically connect to the server:
|
|
48
|
-
// https://github.com/redis/node-redis/blob/master/docs/v3-to-v4.md
|
|
49
|
-
await client.connect();
|
|
50
|
-
await client.ping();
|
|
48
|
+
): NestSessionOptions => {
|
|
49
|
+
const client = new Redis(config.valkeyClientOptions);
|
|
51
50
|
const store = new RedisStore({
|
|
52
51
|
client,
|
|
53
52
|
prefix: config.get('REDIS_PREFIX'),
|
|
54
53
|
});
|
|
55
|
-
return config.getSessionSettings(store);
|
|
54
|
+
return { session: config.getSessionSettings(store) };
|
|
56
55
|
},
|
|
57
56
|
}),
|
|
58
57
|
TypeOrmModule.forRootAsync({
|
|
@@ -65,13 +64,12 @@ Example `app.module` from [makerspace](https://github.huit.harvard.edu/SEAS/make
|
|
|
65
64
|
maxQueryExecutionTime: 1000,
|
|
66
65
|
}),
|
|
67
66
|
}),
|
|
68
|
-
// Add the Redis module
|
|
69
67
|
RedisModule.forRootAsync({
|
|
70
68
|
imports: [ConfigModule],
|
|
71
69
|
inject: [ConfigService],
|
|
72
70
|
useFactory: (config: ConfigService): RedisModuleOptions => ({
|
|
73
71
|
type: 'single',
|
|
74
|
-
url: config.
|
|
72
|
+
url: config.valkeyURL,
|
|
75
73
|
options: {
|
|
76
74
|
tls: {
|
|
77
75
|
rejectUnauthorized: false,
|
|
@@ -79,31 +77,18 @@ Example `app.module` from [makerspace](https://github.huit.harvard.edu/SEAS/make
|
|
|
79
77
|
},
|
|
80
78
|
}),
|
|
81
79
|
}),
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
80
|
+
AdvisorModule,
|
|
81
|
+
StudentModule,
|
|
82
|
+
StudentToAdvisorModule,
|
|
83
|
+
NoteModule,
|
|
84
|
+
ThesisModule,
|
|
85
|
+
DegreeProgramModule,
|
|
86
|
+
MilestonesModule,
|
|
87
|
+
HealthcheckModule.registerAsync({ // Add HealthcheckModule
|
|
89
88
|
imports: [ConfigModule],
|
|
90
89
|
inject: [ConfigService],
|
|
91
90
|
useFactory: (configService: ConfigService): HealthcheckModuleOptions => {
|
|
92
|
-
|
|
93
|
-
if (!casHealthcheckUrl) throw new Error('casCheckURL is not defined or is invalid in configuration.');
|
|
94
|
-
try {
|
|
95
|
-
new URL(casHealthcheckUrl);
|
|
96
|
-
} catch (error) {
|
|
97
|
-
if (error instanceof Error) {
|
|
98
|
-
throw new Error(`casCheckURL is not a valid URL. ${error}`);
|
|
99
|
-
} else {
|
|
100
|
-
throw error;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return {
|
|
104
|
-
...configService.healthcheckConfig,
|
|
105
|
-
casCheckURL: casHealthcheckUrl,
|
|
106
|
-
};
|
|
91
|
+
return configService.healthcheckConfig,
|
|
107
92
|
},
|
|
108
93
|
}),
|
|
109
94
|
],
|
|
@@ -118,7 +103,10 @@ class AppModule implements NestModule {
|
|
|
118
103
|
configure(consumer: MiddlewareConsumer): void {
|
|
119
104
|
consumer
|
|
120
105
|
.apply(LogMiddleware)
|
|
121
|
-
.exclude(
|
|
106
|
+
.exclude(
|
|
107
|
+
'heartbeat',
|
|
108
|
+
{ path: 'oapGradDatabase/health-check', method: RequestMethod.ALL } // exclude healthcheck
|
|
109
|
+
)
|
|
122
110
|
.forRoutes('*');
|
|
123
111
|
}
|
|
124
112
|
}
|
|
@@ -127,33 +115,40 @@ export { AppModule };
|
|
|
127
115
|
|
|
128
116
|
```
|
|
129
117
|
|
|
130
|
-
Examples of additions to `config.service.ts` from [
|
|
118
|
+
Examples of additions to `config.service.ts` from [oap-grad-database](https://github.huit.harvard.edu/SEAS/makerspace/blob/feature/add-healthcheck-module/src/server/config/config.service.ts):
|
|
131
119
|
|
|
132
120
|
```ts
|
|
133
121
|
/**
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
public get
|
|
122
|
+
* Returns the OIDC discovery document URL, which will be sent to the
|
|
123
|
+
* Healthcheck Module. The /.well-known/openid-configuration endpoint
|
|
124
|
+
* is the standard, unauthenticated health signal exposed by all OIDC
|
|
125
|
+
* providers.
|
|
126
|
+
*/
|
|
127
|
+
public get oidcCheckURL(): string {
|
|
128
|
+
return `${this.oidcBaseURL}/.well-known/openid-configuration`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Returns the Valkey URL
|
|
133
|
+
*/
|
|
134
|
+
public get valkeyURL(): string {
|
|
140
135
|
const {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (
|
|
147
|
-
|
|
136
|
+
VALKEY_HOST,
|
|
137
|
+
VALKEY_PORT,
|
|
138
|
+
VALKEY_PASSWORD,
|
|
139
|
+
} = this.env;
|
|
140
|
+
const valkey = new URL(`rediss://${VALKEY_HOST}:${VALKEY_PORT}`);
|
|
141
|
+
if (VALKEY_PASSWORD) {
|
|
142
|
+
valkey.password = VALKEY_PASSWORD;
|
|
148
143
|
}
|
|
149
|
-
return
|
|
144
|
+
return valkey.toString();
|
|
150
145
|
}
|
|
151
146
|
|
|
152
147
|
public get healthcheckConfig(): HealthcheckModuleOptions {
|
|
153
148
|
return {
|
|
154
|
-
|
|
149
|
+
oidcCheckURL: this.oidcCheckURL,
|
|
155
150
|
dbOptions: this.dbOptions,
|
|
156
|
-
|
|
151
|
+
valkeyURL: this.valkeyURL,
|
|
157
152
|
version: this.buildVersion,
|
|
158
153
|
strategy: this.isProduction
|
|
159
154
|
? HEALTH_STRATEGY_NAME.HEALTHCHECK
|
|
@@ -176,7 +171,7 @@ You should receive a response that looks like this:
|
|
|
176
171
|
"harvard-key": {
|
|
177
172
|
"status": "up"
|
|
178
173
|
},
|
|
179
|
-
"
|
|
174
|
+
"valkey": {
|
|
180
175
|
"status": "up",
|
|
181
176
|
"result": "PONG"
|
|
182
177
|
}
|
|
@@ -189,7 +184,7 @@ You should receive a response that looks like this:
|
|
|
189
184
|
"harvard-key": {
|
|
190
185
|
"status": "up"
|
|
191
186
|
},
|
|
192
|
-
"
|
|
187
|
+
"valkey": {
|
|
193
188
|
"status": "up",
|
|
194
189
|
"result": "PONG"
|
|
195
190
|
}
|