@3sixty/api-service 0.0.0-stage → 0.20.0-next.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/LICENSE +201 -0
- package/README.md +24 -2
- package/dist/es/healthApplicationTask.js +151 -0
- package/dist/es/healthApplicationTask.js.map +1 -0
- package/dist/es/healthRoutes.js +125 -0
- package/dist/es/healthRoutes.js.map +1 -0
- package/dist/es/healthService.js +308 -0
- package/dist/es/healthService.js.map +1 -0
- package/dist/es/index.js +15 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/informationRoutes.js +298 -0
- package/dist/es/informationRoutes.js.map +1 -0
- package/dist/es/informationService.js +123 -0
- package/dist/es/informationService.js.map +1 -0
- package/dist/es/models/IHealthServiceConfig.js +4 -0
- package/dist/es/models/IHealthServiceConfig.js.map +1 -0
- package/dist/es/models/IHealthServiceConstructorOptions.js +2 -0
- package/dist/es/models/IHealthServiceConstructorOptions.js.map +1 -0
- package/dist/es/models/IInformationServiceConfig.js +2 -0
- package/dist/es/models/IInformationServiceConfig.js.map +1 -0
- package/dist/es/models/IInformationServiceConstructorOptions.js +2 -0
- package/dist/es/models/IInformationServiceConstructorOptions.js.map +1 -0
- package/dist/es/models/IPlatformServiceConfig.js +4 -0
- package/dist/es/models/IPlatformServiceConfig.js.map +1 -0
- package/dist/es/models/IPlatformServiceConstructorOptions.js +2 -0
- package/dist/es/models/IPlatformServiceConstructorOptions.js.map +1 -0
- package/dist/es/platformService.js +211 -0
- package/dist/es/platformService.js.map +1 -0
- package/dist/es/restEntryPoints.js +20 -0
- package/dist/es/restEntryPoints.js.map +1 -0
- package/dist/types/healthApplicationTask.d.ts +20 -0
- package/dist/types/healthRoutes.d.ts +20 -0
- package/dist/types/healthService.d.ts +42 -0
- package/dist/types/index.d.ts +12 -0
- package/dist/types/informationRoutes.d.ts +60 -0
- package/dist/types/informationService.d.ts +60 -0
- package/dist/types/models/IHealthServiceConfig.d.ts +37 -0
- package/dist/types/models/IHealthServiceConstructorOptions.d.ts +15 -0
- package/dist/types/models/IInformationServiceConfig.d.ts +18 -0
- package/dist/types/models/IInformationServiceConstructorOptions.d.ts +10 -0
- package/dist/types/models/IPlatformServiceConfig.d.ts +10 -0
- package/dist/types/models/IPlatformServiceConstructorOptions.d.ts +19 -0
- package/dist/types/platformService.d.ts +57 -0
- package/dist/types/restEntryPoints.d.ts +5 -0
- package/docs/changelog.md +17 -0
- package/docs/examples.md +74 -0
- package/docs/open-api/spec.json +712 -0
- package/docs/reference/classes/HealthService.md +127 -0
- package/docs/reference/classes/InformationService.md +179 -0
- package/docs/reference/classes/PlatformService.md +209 -0
- package/docs/reference/functions/generateRestRoutesHealth.md +25 -0
- package/docs/reference/functions/generateRestRoutesInformation.md +25 -0
- package/docs/reference/functions/serverFavIcon.md +31 -0
- package/docs/reference/functions/serverHealth.md +31 -0
- package/docs/reference/functions/serverInfo.md +31 -0
- package/docs/reference/functions/serverLivez.md +31 -0
- package/docs/reference/functions/serverReadyz.md +31 -0
- package/docs/reference/functions/serverRoot.md +31 -0
- package/docs/reference/functions/serverSpec.md +31 -0
- package/docs/reference/index.md +34 -0
- package/docs/reference/interfaces/IHealthServiceConfig.md +79 -0
- package/docs/reference/interfaces/IHealthServiceConstructorOptions.md +25 -0
- package/docs/reference/interfaces/IInformationServiceConfig.md +27 -0
- package/docs/reference/interfaces/IInformationServiceConstructorOptions.md +11 -0
- package/docs/reference/interfaces/IPlatformServiceConfig.md +17 -0
- package/docs/reference/interfaces/IPlatformServiceConstructorOptions.md +33 -0
- package/docs/reference/variables/restEntryPoints.md +5 -0
- package/docs/reference/variables/tagsHealth.md +5 -0
- package/docs/reference/variables/tagsInformation.md +5 -0
- package/locales/en.json +14 -0
- package/package.json +57 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2024 IOTA Stiftung.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 3Sixty API Service
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This package provides information and hosting service implementations with generated REST route handlers.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
npm install @3sixty/api-service
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Examples
|
|
12
|
+
|
|
13
|
+
Usage of the APIs is shown in the examples [docs/examples.md](docs/examples.md)
|
|
14
|
+
|
|
15
|
+
## Reference
|
|
16
|
+
|
|
17
|
+
Detailed reference documentation for the API can be found in [docs/reference/index.md](docs/reference/index.md)
|
|
18
|
+
|
|
19
|
+
## Changelog
|
|
20
|
+
|
|
21
|
+
The changes between each version can be found in [docs/changelog.md](docs/changelog.md)
|
|
22
|
+
|
|
23
|
+
## Origin
|
|
24
|
+
|
|
25
|
+
This package is derived from the original [iotaledger/twin-api](https://github.com/iotaledger/twin-api/tree/next/packages/api-service) repository.
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { ContextIdKeys, ContextIdStore } from "@3sixty/context";
|
|
2
|
+
import { GeneralError, Is } from "@3sixty/core";
|
|
3
|
+
import { ModuleHelper } from "@3sixty/modules";
|
|
4
|
+
let engine;
|
|
5
|
+
let startupPromise;
|
|
6
|
+
let excludeCloneComponents;
|
|
7
|
+
/**
|
|
8
|
+
* Start the engine clone used for application health checks.
|
|
9
|
+
* @param engineCloneData The engine clone data supplied automatically by the background task framework.
|
|
10
|
+
* @param excludeComponents Verified regular expression patterns for component types to exclude
|
|
11
|
+
* from the clone, validated by the health service before registration.
|
|
12
|
+
*/
|
|
13
|
+
export async function healthApplicationTaskStart(engineCloneData, excludeComponents) {
|
|
14
|
+
excludeCloneComponents = excludeComponents;
|
|
15
|
+
startupPromise = (async () => {
|
|
16
|
+
if (!Is.empty(engineCloneData)) {
|
|
17
|
+
const cloneData = Is.object(engineCloneData) && Is.arrayValue(excludeComponents)
|
|
18
|
+
? await ModuleHelper.execModuleMethod("@3sixty/engine-models", "EngineCloneHelper.filterCloneComponents", [engineCloneData, excludeComponents])
|
|
19
|
+
: engineCloneData;
|
|
20
|
+
const clone = await ModuleHelper.execModuleMethod("@3sixty/engine-core", "EngineCoreBuilder.fromClone", [
|
|
21
|
+
"engine",
|
|
22
|
+
cloneData,
|
|
23
|
+
await ContextIdStore.getContextIds(),
|
|
24
|
+
{ logLevel: "error" }
|
|
25
|
+
]);
|
|
26
|
+
if (Is.empty(clone)) {
|
|
27
|
+
throw new GeneralError("applicationHealthTask", "engineNotStarted");
|
|
28
|
+
}
|
|
29
|
+
await clone.start();
|
|
30
|
+
engine = clone;
|
|
31
|
+
}
|
|
32
|
+
})();
|
|
33
|
+
try {
|
|
34
|
+
await startupPromise;
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
startupPromise = undefined;
|
|
38
|
+
throw err;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Stop the engine clone used for application health checks.
|
|
43
|
+
*/
|
|
44
|
+
export async function healthApplicationTaskEnd() {
|
|
45
|
+
if (!Is.empty(engine)) {
|
|
46
|
+
await engine.stop();
|
|
47
|
+
engine = undefined;
|
|
48
|
+
}
|
|
49
|
+
startupPromise = undefined;
|
|
50
|
+
excludeCloneComponents = undefined;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Execute the application health lifecycle (init, application, teardown) across all registered
|
|
54
|
+
* components. Called by the background task framework in a worker thread; the engine clone data
|
|
55
|
+
* is used to initialise a local clone of the engine so component factories are available.
|
|
56
|
+
* @param engineCloneData The engine clone data supplied automatically by the background task framework.
|
|
57
|
+
* @returns The health entries collected across all three passes.
|
|
58
|
+
*/
|
|
59
|
+
export async function healthApplicationTask(engineCloneData) {
|
|
60
|
+
if (Is.empty(engineCloneData)) {
|
|
61
|
+
return [];
|
|
62
|
+
}
|
|
63
|
+
if (startupPromise) {
|
|
64
|
+
try {
|
|
65
|
+
await startupPromise;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
startupPromise = undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (Is.empty(engine)) {
|
|
72
|
+
await healthApplicationTaskStart(engineCloneData, excludeCloneComponents);
|
|
73
|
+
}
|
|
74
|
+
return executeApplicationHealthCycle();
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Run the application health lifecycle for each registered provider: initialise shared context,
|
|
78
|
+
* run the health checks, then tear down the per-check context in reverse registration order.
|
|
79
|
+
* @returns The health entries collected for the current application health pass.
|
|
80
|
+
*/
|
|
81
|
+
async function executeApplicationHealthCycle() {
|
|
82
|
+
if (Is.empty(engine)) {
|
|
83
|
+
throw new GeneralError("applicationHealthTask", "engineNotStarted");
|
|
84
|
+
}
|
|
85
|
+
const engineContextIds = engine.getContextIds() ?? {};
|
|
86
|
+
const registeredInstances = await engine.getRegisteredComponents();
|
|
87
|
+
// Pass 1: Init - providers populate healthContextIds with any IDs they establish
|
|
88
|
+
const healthContextIds = {
|
|
89
|
+
// Only use Node from the main engine context.
|
|
90
|
+
// All other keys should be provided by healthApplicationInit of each component
|
|
91
|
+
// as we don't want to use real ids.
|
|
92
|
+
[ContextIdKeys.Node]: engineContextIds[ContextIdKeys.Node]
|
|
93
|
+
};
|
|
94
|
+
for (const registeredInstance of registeredInstances) {
|
|
95
|
+
if (Is.object(registeredInstance.component)) {
|
|
96
|
+
const initMethod = registeredInstance.component.healthApplicationInit?.bind(registeredInstance.component);
|
|
97
|
+
if (Is.function(initMethod)) {
|
|
98
|
+
await initMethod(healthContextIds);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// Pass 2: Application health check wrapped in combined engine + init context
|
|
103
|
+
const allHealth = [];
|
|
104
|
+
const lazyPromises = [];
|
|
105
|
+
await ContextIdStore.run(healthContextIds, async () => {
|
|
106
|
+
for (const registeredInstance of registeredInstances) {
|
|
107
|
+
if (Is.object(registeredInstance.component)) {
|
|
108
|
+
const healthMethod = registeredInstance.component.healthApplication?.bind(registeredInstance.component);
|
|
109
|
+
if (Is.function(healthMethod)) {
|
|
110
|
+
let fired = false;
|
|
111
|
+
let callback = async () => { };
|
|
112
|
+
const lazyPromise = new Promise(resolve => {
|
|
113
|
+
callback = async (result) => {
|
|
114
|
+
if (!fired) {
|
|
115
|
+
fired = true;
|
|
116
|
+
allHealth.push(...result);
|
|
117
|
+
resolve();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
const result = await healthMethod(callback);
|
|
122
|
+
// undefined result indicates that the component will provide the
|
|
123
|
+
// result asynchronously via the callback
|
|
124
|
+
if (Is.undefined(result)) {
|
|
125
|
+
lazyPromises.push(lazyPromise);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
allHealth.push(...result);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
// Wait for all deferred callbacks before proceeding to teardown
|
|
135
|
+
if (lazyPromises.length > 0) {
|
|
136
|
+
await Promise.allSettled(lazyPromises);
|
|
137
|
+
}
|
|
138
|
+
// Pass 3: Teardown wrapped in combined engine + init context
|
|
139
|
+
await ContextIdStore.run(healthContextIds, async () => {
|
|
140
|
+
for (const registeredInstance of registeredInstances.slice().reverse()) {
|
|
141
|
+
if (Is.object(registeredInstance.component)) {
|
|
142
|
+
const teardownMethod = registeredInstance.component.healthApplicationTeardown?.bind(registeredInstance.component);
|
|
143
|
+
if (Is.function(teardownMethod)) {
|
|
144
|
+
await teardownMethod();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
return allHealth;
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=healthApplicationTask.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"healthApplicationTask.js","sources":["../../src/healthApplicationTask.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEhE,OAAO,EAAE,YAAY,EAAE,EAAE,EAAE,MAAM,cAAc,CAAC;AAEhD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,IAAI,MAOQ,CAAC;AACb,IAAI,cAAyC,CAAC;AAC9C,IAAI,sBAA4C,CAAC;AAEjD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC/C,eAAwB,EACxB,iBAA4B;IAE5B,sBAAsB,GAAG,iBAAiB,CAAC;IAE3C,cAAc,GAAG,CAAC,KAAK,IAAI,EAAE;QAC5B,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,eAAe,CAAC,EAAE,CAAC;YAChC,MAAM,SAAS,GACd,EAAE,CAAC,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE,CAAC,UAAU,CAAC,iBAAiB,CAAC;gBAC7D,CAAC,CAAC,MAAM,YAAY,CAAC,gBAAgB,CACnC,uBAAuB,EACvB,yCAAyC,EACzC,CAAC,eAAe,EAAE,iBAAiB,CAAC,CACpC;gBACF,CAAC,CAAC,eAAe,CAAC;YACpB,MAAM,KAAK,GAAG,MAAM,YAAY,CAAC,gBAAgB,CAK9C,qBAAqB,EAAE,6BAA6B,EAAE;gBACxD,QAAQ;gBACR,SAAS;gBACT,MAAM,cAAc,CAAC,aAAa,EAAE;gBACpC,EAAE,QAAQ,EAAE,OAAO,EAAE;aACrB,CAAC,CAAC;YACH,IAAI,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;gBACrB,MAAM,I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,CAC5B,CAAC;gBACF,IAAI,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC;oBACjC,MAAM,cAAc,EAAE,CAAC;gBACxB,CAAC;YACF,CAAC;QACF,CAAC;IACF,CAAC,CAAC,CAAC;IAEH,OAAO,SAAS,CAAC;AAClB,CAAC","sourcesContent":["// Copyright 2026 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type {\n\tHealthApplicationCallback,\n\tIHealth,\n\tIHealthProviderComponent\n} from \"@3sixty/api-models\";\nimport { ContextIdKeys, ContextIdStore } from \"@3sixty/context\";\nimport type { IContextIds } from \"@3sixty/context\";\nimport { GeneralError, Is } from \"@3sixty/core\";\nimport type { IComponent } from \"@3sixty/core\";\nimport { ModuleHelper } from \"@3sixty/modules\";\n\nlet engine:\n\t| {\n\t\t\tstart: () => Promise<void>;\n\t\t\tstop: () => Promise<void>;\n\t\t\tgetContextIds: () => IContextIds | undefined;\n\t\t\tgetRegisteredComponents: () => Promise<{ instanceType: string; component: IComponent }[]>;\n\t }\n\t| undefined;\nlet startupPromise: Promise<void> | undefined;\nlet excludeCloneComponents: string[] | undefined;\n\n/**\n * Start the engine clone used for application health checks.\n * @param engineCloneData The engine clone data supplied automatically by the background task framework.\n * @param excludeComponents Verified regular expression patterns for component types to exclude\n * from the clone, validated by the health service before registration.\n */\nexport async function healthApplicationTaskStart(\n\tengineCloneData: unknown,\n\texcludeComponents?: string[]\n): Promise<void> {\n\texcludeCloneComponents = excludeComponents;\n\n\tstartupPromise = (async () => {\n\t\tif (!Is.empty(engineCloneData)) {\n\t\t\tconst cloneData =\n\t\t\t\tIs.object(engineCloneData) && Is.arrayValue(excludeComponents)\n\t\t\t\t\t? await ModuleHelper.execModuleMethod<unknown>(\n\t\t\t\t\t\t\t\"@3sixty/engine-models\",\n\t\t\t\t\t\t\t\"EngineCloneHelper.filterCloneComponents\",\n\t\t\t\t\t\t\t[engineCloneData, excludeComponents]\n\t\t\t\t\t\t)\n\t\t\t\t\t: engineCloneData;\n\t\t\tconst clone = await ModuleHelper.execModuleMethod<{\n\t\t\t\tstart: () => Promise<void>;\n\t\t\t\tstop: () => Promise<void>;\n\t\t\t\tgetContextIds: () => IContextIds | undefined;\n\t\t\t\tgetRegisteredComponents: () => Promise<{ instanceType: string; component: IComponent }[]>;\n\t\t\t}>(\"@3sixty/engine-core\", \"EngineCoreBuilder.fromClone\", [\n\t\t\t\t\"engine\",\n\t\t\t\tcloneData,\n\t\t\t\tawait ContextIdStore.getContextIds(),\n\t\t\t\t{ logLevel: \"error\" }\n\t\t\t]);\n\t\t\tif (Is.empty(clone)) {\n\t\t\t\tthrow new GeneralError(\"applicationHealthTask\", \"engineNotStarted\");\n\t\t\t}\n\t\t\tawait clone.start();\n\t\t\tengine = clone;\n\t\t}\n\t})();\n\n\ttry {\n\t\tawait startupPromise;\n\t} catch (err) {\n\t\tstartupPromise = undefined;\n\t\tthrow err;\n\t}\n}\n\n/**\n * Stop the engine clone used for application health checks.\n */\nexport async function healthApplicationTaskEnd(): Promise<void> {\n\tif (!Is.empty(engine)) {\n\t\tawait engine.stop();\n\t\tengine = undefined;\n\t}\n\tstartupPromise = undefined;\n\texcludeCloneComponents = undefined;\n}\n\n/**\n * Execute the application health lifecycle (init, application, teardown) across all registered\n * components. Called by the background task framework in a worker thread; the engine clone data\n * is used to initialise a local clone of the engine so component factories are available.\n * @param engineCloneData The engine clone data supplied automatically by the background task framework.\n * @returns The health entries collected across all three passes.\n */\nexport async function healthApplicationTask(engineCloneData: unknown): Promise<IHealth[]> {\n\tif (Is.empty(engineCloneData)) {\n\t\treturn [];\n\t}\n\n\tif (startupPromise) {\n\t\ttry {\n\t\t\tawait startupPromise;\n\t\t} catch {\n\t\t\tstartupPromise = undefined;\n\t\t}\n\t}\n\n\tif (Is.empty(engine)) {\n\t\tawait healthApplicationTaskStart(engineCloneData, excludeCloneComponents);\n\t}\n\n\treturn executeApplicationHealthCycle();\n}\n\n/**\n * Run the application health lifecycle for each registered provider: initialise shared context,\n * run the health checks, then tear down the per-check context in reverse registration order.\n * @returns The health entries collected for the current application health pass.\n */\nasync function executeApplicationHealthCycle(): Promise<IHealth[]> {\n\tif (Is.empty(engine)) {\n\t\tthrow new GeneralError(\"applicationHealthTask\", \"engineNotStarted\");\n\t}\n\n\tconst engineContextIds = engine.getContextIds() ?? {};\n\tconst registeredInstances = await engine.getRegisteredComponents();\n\n\t// Pass 1: Init - providers populate healthContextIds with any IDs they establish\n\tconst healthContextIds: IContextIds = {\n\t\t// Only use Node from the main engine context.\n\t\t// All other keys should be provided by healthApplicationInit of each component\n\t\t// as we don't want to use real ids.\n\t\t[ContextIdKeys.Node]: engineContextIds[ContextIdKeys.Node]\n\t};\n\tfor (const registeredInstance of registeredInstances) {\n\t\tif (Is.object<IHealthProviderComponent>(registeredInstance.component)) {\n\t\t\tconst initMethod = registeredInstance.component.healthApplicationInit?.bind(\n\t\t\t\tregisteredInstance.component\n\t\t\t);\n\t\t\tif (Is.function(initMethod)) {\n\t\t\t\tawait initMethod(healthContextIds);\n\t\t\t}\n\t\t}\n\t}\n\n\t// Pass 2: Application health check wrapped in combined engine + init context\n\tconst allHealth: IHealth[] = [];\n\tconst lazyPromises: Promise<void>[] = [];\n\n\tawait ContextIdStore.run(healthContextIds, async () => {\n\t\tfor (const registeredInstance of registeredInstances) {\n\t\t\tif (Is.object<IHealthProviderComponent>(registeredInstance.component)) {\n\t\t\t\tconst healthMethod = registeredInstance.component.healthApplication?.bind(\n\t\t\t\t\tregisteredInstance.component\n\t\t\t\t);\n\t\t\t\tif (Is.function(healthMethod)) {\n\t\t\t\t\tlet fired = false;\n\t\t\t\t\tlet callback: HealthApplicationCallback = async () => {};\n\t\t\t\t\tconst lazyPromise = new Promise<void>(resolve => {\n\t\t\t\t\t\tcallback = async (result: IHealth[]) => {\n\t\t\t\t\t\t\tif (!fired) {\n\t\t\t\t\t\t\t\tfired = true;\n\t\t\t\t\t\t\t\tallHealth.push(...result);\n\t\t\t\t\t\t\t\tresolve();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t};\n\t\t\t\t\t});\n\n\t\t\t\t\tconst result = await healthMethod(callback);\n\t\t\t\t\t// undefined result indicates that the component will provide the\n\t\t\t\t\t// result asynchronously via the callback\n\t\t\t\t\tif (Is.undefined(result)) {\n\t\t\t\t\t\tlazyPromises.push(lazyPromise);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tallHealth.push(...result);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t});\n\n\t// Wait for all deferred callbacks before proceeding to teardown\n\tif (lazyPromises.length > 0) {\n\t\tawait Promise.allSettled(lazyPromises);\n\t}\n\n\t// Pass 3: Teardown wrapped in combined engine + init context\n\tawait ContextIdStore.run(healthContextIds, async () => {\n\t\tfor (const registeredInstance of registeredInstances.slice().reverse()) {\n\t\t\tif (Is.object<IHealthProviderComponent>(registeredInstance.component)) {\n\t\t\t\tconst teardownMethod = registeredInstance.component.healthApplicationTeardown?.bind(\n\t\t\t\t\tregisteredInstance.component\n\t\t\t\t);\n\t\t\t\tif (Is.function(teardownMethod)) {\n\t\t\t\t\tawait teardownMethod();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t});\n\n\treturn allHealth;\n}\n"]}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Copyright 2024 IOTA Stiftung.
|
|
2
|
+
// Copyright 2026 3Sixty Trade Ltd.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
4
|
+
import { HealthCategory, HealthStatus } from "@3sixty/api-models";
|
|
5
|
+
import { ComponentFactory } from "@3sixty/core";
|
|
6
|
+
/**
|
|
7
|
+
* The tag to associate with the routes.
|
|
8
|
+
*/
|
|
9
|
+
export const tagsHealth = [
|
|
10
|
+
{
|
|
11
|
+
name: "Health",
|
|
12
|
+
description: "Health endpoints for the REST server."
|
|
13
|
+
}
|
|
14
|
+
];
|
|
15
|
+
/**
|
|
16
|
+
* The default authorization for the routes, used to seed authorization rules.
|
|
17
|
+
*/
|
|
18
|
+
const DEFAULT_AUTHORIZATION_READER = {
|
|
19
|
+
permission: "health:read",
|
|
20
|
+
role: "devops"
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The REST routes for server health.
|
|
24
|
+
* @param baseRouteName Prefix to prepend to the paths.
|
|
25
|
+
* @param componentName The name of the component to use in the routes stored in the ComponentFactory.
|
|
26
|
+
* @returns The generated routes.
|
|
27
|
+
*/
|
|
28
|
+
export function generateRestRoutesHealth(baseRouteName, componentName) {
|
|
29
|
+
const healthRoute = {
|
|
30
|
+
operationId: "serverHealth",
|
|
31
|
+
summary: "Get the health for the server",
|
|
32
|
+
tag: tagsHealth[0].name,
|
|
33
|
+
method: "GET",
|
|
34
|
+
path: `${baseRouteName}/`,
|
|
35
|
+
handler: async (httpRequestContext, request) => serverHealth(httpRequestContext, componentName, request),
|
|
36
|
+
responseType: [
|
|
37
|
+
{
|
|
38
|
+
type: "IServerHealthResponse",
|
|
39
|
+
examples: [
|
|
40
|
+
{
|
|
41
|
+
id: "healthResponseOK",
|
|
42
|
+
description: "The response for the health request.",
|
|
43
|
+
response: {
|
|
44
|
+
body: {
|
|
45
|
+
status: HealthStatus.Ok,
|
|
46
|
+
components: [
|
|
47
|
+
{
|
|
48
|
+
source: "Database",
|
|
49
|
+
status: HealthStatus.Ok,
|
|
50
|
+
category: HealthCategory.Connectivity
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
source: "Storage",
|
|
54
|
+
status: HealthStatus.Ok,
|
|
55
|
+
category: HealthCategory.Connectivity
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
id: "healthResponseWarning",
|
|
63
|
+
description: "The response for the health request with warnings.",
|
|
64
|
+
response: {
|
|
65
|
+
body: {
|
|
66
|
+
status: HealthStatus.Warning,
|
|
67
|
+
components: [
|
|
68
|
+
{
|
|
69
|
+
source: "Database",
|
|
70
|
+
status: HealthStatus.Warning,
|
|
71
|
+
description: "slowRunning",
|
|
72
|
+
category: HealthCategory.Connectivity
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
source: "Storage",
|
|
76
|
+
status: HealthStatus.Ok,
|
|
77
|
+
category: HealthCategory.Connectivity
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
id: "healthResponseError",
|
|
85
|
+
description: "The response for the health request with errors.",
|
|
86
|
+
response: {
|
|
87
|
+
body: {
|
|
88
|
+
status: HealthStatus.Error,
|
|
89
|
+
components: [
|
|
90
|
+
{
|
|
91
|
+
source: "Database",
|
|
92
|
+
status: HealthStatus.Ok,
|
|
93
|
+
category: HealthCategory.Connectivity
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
source: "Storage",
|
|
97
|
+
status: HealthStatus.Error,
|
|
98
|
+
description: "storageFull",
|
|
99
|
+
category: HealthCategory.Connectivity
|
|
100
|
+
}
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
],
|
|
108
|
+
defaultAuthorization: DEFAULT_AUTHORIZATION_READER
|
|
109
|
+
};
|
|
110
|
+
return [healthRoute];
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Get the health for the server.
|
|
114
|
+
* @param httpRequestContext The request context for the API.
|
|
115
|
+
* @param componentName The name of the component to use in the routes.
|
|
116
|
+
* @param request The request.
|
|
117
|
+
* @returns The response object with additional http response properties.
|
|
118
|
+
*/
|
|
119
|
+
export async function serverHealth(httpRequestContext, componentName, request) {
|
|
120
|
+
const component = ComponentFactory.get(componentName);
|
|
121
|
+
return {
|
|
122
|
+
body: await component.healthStatus()
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=healthRoutes.js.map
|
|
@@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O;QACN,IAAI,EAAE,MAAM,SAAS,CAAC,YAAY,EAAE;KACpC,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// Copyright 2026 3Sixty Trade Ltd.\n// SPDX-License-Identifier: Apache-2.0.\nimport {\n\tHealthCategory,\n\tHealthStatus,\n\ttype IHealthComponent,\n\ttype IHttpRequestContext,\n\ttype INoContentRequest,\n\ttype IRestRoute,\n\ttype IServerHealthResponse,\n\ttype ITag,\n\ttype IRouteAuthorization\n} from \"@3sixty/api-models\";\nimport { ComponentFactory } from \"@3sixty/core\";\n\n\n/**\n * The tag to associate with the routes.\n */\nexport const tagsHealth: ITag[] = [\n\t{\n\t\tname: \"Health\",\n\t\tdescription: \"Health endpoints for the REST server.\"\n\t}\n];\n\n/**\n * The default authorization for the routes, used to seed authorization rules.\n */\nconst DEFAULT_AUTHORIZATION_READER: IRouteAuthorization = {\n\tpermission: \"health:read\",\n\trole: \"devops\"\n};\n\n/**\n * The REST routes for server health.\n * @param baseRouteName Prefix to prepend to the paths.\n * @param componentName The name of the component to use in the routes stored in the ComponentFactory.\n * @returns The generated routes.\n */\nexport function generateRestRoutesHealth(\n\tbaseRouteName: string,\n\tcomponentName: string\n): IRestRoute[] {\n\tconst healthRoute: IRestRoute<INoContentRequest, IServerHealthResponse> = {\n\t\toperationId: \"serverHealth\",\n\t\tsummary: \"Get the health for the server\",\n\t\ttag: tagsHealth[0].name,\n\t\tmethod: \"GET\",\n\t\tpath: `${baseRouteName}/`,\n\t\thandler: async (httpRequestContext, request) =>\n\t\t\tserverHealth(httpRequestContext, componentName, request),\n\t\tresponseType: [\n\t\t\t{\n\t\t\t\ttype: \"IServerHealthResponse\",\n\t\t\t\texamples: [\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"healthResponseOK\",\n\t\t\t\t\t\tdescription: \"The response for the health request.\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tstatus: HealthStatus.Ok,\n\t\t\t\t\t\t\t\tcomponents: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tsource: \"Database\",\n\t\t\t\t\t\t\t\t\t\tstatus: HealthStatus.Ok,\n\t\t\t\t\t\t\t\t\t\tcategory: HealthCategory.Connectivity\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tsource: \"Storage\",\n\t\t\t\t\t\t\t\t\t\tstatus: HealthStatus.Ok,\n\t\t\t\t\t\t\t\t\t\tcategory: HealthCategory.Connectivity\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"healthResponseWarning\",\n\t\t\t\t\t\tdescription: \"The response for the health request with warnings.\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tstatus: HealthStatus.Warning,\n\t\t\t\t\t\t\t\tcomponents: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tsource: \"Database\",\n\t\t\t\t\t\t\t\t\t\tstatus: HealthStatus.Warning,\n\t\t\t\t\t\t\t\t\t\tdescription: \"slowRunning\",\n\t\t\t\t\t\t\t\t\t\tcategory: HealthCategory.Connectivity\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tsource: \"Storage\",\n\t\t\t\t\t\t\t\t\t\tstatus: HealthStatus.Ok,\n\t\t\t\t\t\t\t\t\t\tcategory: HealthCategory.Connectivity\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tid: \"healthResponseError\",\n\t\t\t\t\t\tdescription: \"The response for the health request with errors.\",\n\t\t\t\t\t\tresponse: {\n\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\tstatus: HealthStatus.Error,\n\t\t\t\t\t\t\t\tcomponents: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tsource: \"Database\",\n\t\t\t\t\t\t\t\t\t\tstatus: HealthStatus.Ok,\n\t\t\t\t\t\t\t\t\t\tcategory: HealthCategory.Connectivity\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tsource: \"Storage\",\n\t\t\t\t\t\t\t\t\t\tstatus: HealthStatus.Error,\n\t\t\t\t\t\t\t\t\t\tdescription: \"storageFull\",\n\t\t\t\t\t\t\t\t\t\tcategory: HealthCategory.Connectivity\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t]\n\t\t\t}\n\t\t],\n\t\tdefaultAuthorization: DEFAULT_AUTHORIZATION_READER\n\t};\n\n\treturn [healthRoute];\n}\n\n/**\n * Get the health for the server.\n * @param httpRequestContext The request context for the API.\n * @param componentName The name of the component to use in the routes.\n * @param request The request.\n * @returns The response object with additional http response properties.\n */\nexport async function serverHealth(\n\thttpRequestContext: IHttpRequestContext,\n\tcomponentName: string,\n\trequest: INoContentRequest\n): Promise<IServerHealthResponse> {\n\tconst component = ComponentFactory.get<IHealthComponent>(componentName);\n\treturn {\n\t\tbody: await component.healthStatus()\n\t};\n}\n"]}
|