Repository navigation
Expand file tree
/
Copy pathIndex.ts
More file actions
165 lines (153 loc) · 5.15 KB
/
Copy pathIndex.ts
File metadata and controls
165 lines (153 loc) · 5.15 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import EnterpriseServerModule, {
AuditLogRecorder,
ENTERPRISE_SERVER_MODULE_NAME,
ProjectBillingHooks,
} from "Common/Server/Enterprise/EnterpriseServerModule";
import type BillingProvider from "Common/Server/Types/Billing/BillingProvider";
import type { ExpressRouter } from "Common/Server/Utils/Express";
import { ProductBranding } from "Common/Types/Branding/ProductBranding";
import { SessionLengths } from "Common/Types/UserSession/SessionLength";
import EnterpriseArea from "./Types/EnterpriseArea";
import AdminHealthArea, { getAdminHealthRouter } from "./AdminHealth/Index";
import AuditLogArea, { getAuditLogRecorder } from "./AuditLog/Index";
import AuditLogExportArea from "./AuditLogExport/Index";
import BackupArea from "./Backup/Index";
import BillingArea, { getBillingProvider } from "./Billing/Index";
import CustomNotificationChannelsArea from "./CustomNotificationChannels/Index";
import CloudMarketplaceArea, {
getProjectBillingHooks,
} from "./CloudMarketplace/Index";
import IdentityArea from "./Identity/Index";
import LicenseArea, { licensing } from "./License/Index";
import LicenseServerArea from "./LicenseServer/Index";
import MarketplaceArea from "./Marketplace/Index";
import SessionLengthArea, {
forgetUserSessionLengths,
getUserSessionLengths,
} from "./SessionLength/Index";
import TeamComplianceArea from "./TeamCompliance/Index";
import WhiteLabelArea, {
getProductBranding,
getProductBrandingLogo,
} from "./WhiteLabel/Index";
import WorkersArea from "./Workers/Index";
import packageJson from "../package.json";
/*
* The OneUptime Enterprise Edition server module. packages/App/Utils/
* EnterpriseLoader.ts require()s this file at boot, checks the default export
* against EnterpriseServerModule and registers it with EnterpriseEdition.
*
* It is assembled from one module per area so each area is owned and edited
* on its own; this file only decides the order they run in.
*/
/*
* License first: its init loads the license snapshot the other areas' checks
* read. This order is also the router mount order.
*/
export const ENTERPRISE_AREAS: ReadonlyArray<EnterpriseArea> = [
LicenseArea,
IdentityArea,
TeamComplianceArea,
AuditLogArea,
LicenseServerArea,
AdminHealthArea,
WorkersArea,
WhiteLabelArea,
MarketplaceArea,
AuditLogExportArea,
BackupArea,
CustomNotificationChannelsArea,
SessionLengthArea,
// OneUptime Cloud's billing areas stay last: their routers mount after every other area's.
BillingArea,
CloudMarketplaceArea,
];
type RouterHook = (area: EnterpriseArea) => Array<ExpressRouter> | undefined;
type AsyncHook = (area: EnterpriseArea) => Promise<void> | undefined;
const collectRouters: (hook: RouterHook) => Array<ExpressRouter> = (
hook: RouterHook,
): Array<ExpressRouter> => {
const routers: Array<ExpressRouter> = [];
for (const area of ENTERPRISE_AREAS) {
routers.push(...(hook(area) || []));
}
return routers;
};
/*
* Runs one async hook of every area, in order. One area failing does not stop
* the others; the failures are reported together afterwards, and the loader
* logs them without stopping the boot.
*/
const runForEveryArea: (
hookName: string,
hook: AsyncHook,
) => Promise<void> = async (
hookName: string,
hook: AsyncHook,
): Promise<void> => {
const failures: Array<string> = [];
for (const area of ENTERPRISE_AREAS) {
try {
await hook(area);
} catch (err) {
failures.push(
`${area.name}: ${err instanceof Error ? err.message : String(err)}`,
);
}
}
if (failures.length > 0) {
throw new Error(
`Enterprise ${hookName} failed in ${failures.length} area(s): ${failures.join("; ")}`,
);
}
};
const EnterpriseModule: EnterpriseServerModule = {
name: ENTERPRISE_SERVER_MODULE_NAME,
version: packageJson.version,
init: async (): Promise<void> => {
await runForEveryArea("init", (area: EnterpriseArea) => {
return area.init?.();
});
},
licensing,
getIdentityRouters: (): Array<ExpressRouter> => {
return collectRouters((area: EnterpriseArea) => {
return area.getIdentityRouters?.();
});
},
getApiRouters: (): Array<ExpressRouter> => {
return collectRouters((area: EnterpriseArea) => {
return area.getApiRouters?.();
});
},
getAdminHealthRouter: (): ExpressRouter | null => {
return getAdminHealthRouter();
},
registerWorkerJobs: async (): Promise<void> => {
await runForEveryArea("worker job registration", (area: EnterpriseArea) => {
return area.registerWorkerJobs?.();
});
},
getAuditLogRecorder: (): AuditLogRecorder | null => {
return getAuditLogRecorder();
},
getProductBranding: (): ProductBranding | null => {
return getProductBranding();
},
getProductBrandingLogo: (): Buffer | null => {
return getProductBrandingLogo();
},
getBillingProvider: (): BillingProvider | null => {
return getBillingProvider();
},
getProjectBillingHooks: (): ProjectBillingHooks | null => {
return getProjectBillingHooks();
},
getUserSessionLengths: (): Promise<SessionLengths> => {
return getUserSessionLengths();
},
forgetUserSessionLengths: (): void => {
forgetUserSessionLengths();
},
};
export default EnterpriseModule;