-
Notifications
You must be signed in to change notification settings - Fork 2
/
handleRequest.ts
236 lines (216 loc) · 11 KB
/
handleRequest.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { Message } from "rs-core/Message.ts";
import { Source } from "rs-core/Source.ts";
import { IServiceConfig, PrePost } from "rs-core/IServiceConfig.ts";
import { config } from "./config.ts";
import { IRawServicesConfig, Tenant } from "./tenant.ts";
import { Url } from "rs-core/Url.ts";
import { slashTrim } from "rs-core/utility/utility.ts";
import { MessageFunction } from "rs-core/Service.ts";
import { BaseContext, contextLoggerArgs } from "rs-core/ServiceContext.ts";
const tenantLoads = {} as Record<string, Promise<void>>;
const wait = (ms: number) => new Promise(res => setTimeout(res, ms));
const tenantLoadTimeoutMs = 500000;
const getTenant = async (url: Url, requestTenant: string) => {
const tenantLoad = tenantLoads[requestTenant];
// if we're not the first getTenant call for this tenant, wait for the first call to complete
// unless we're triggered as part of the first call i.e. source is internal
if (tenantLoad !== undefined) {
const urlIsReady = config.tenants[requestTenant]?.pathIsReady(url);
if (!urlIsReady) await tenantLoads[requestTenant];
}
if (!config.tenants[requestTenant]) {
// the resolve function which resolves the promise on which any subsequent getTenant calls
// will block until the first getTenant call for a tenant succeeds, to ensure only the first
// getTenant actually does the tenant loading.
let resolveLoad = null as (() => void) | null;
let timeoutHandle = null as number | null;
try {
config.logger.debug(`Start -- load tenant ${requestTenant}`, requestTenant);
tenantLoads[requestTenant] = new Promise<void>(resolve => resolveLoad = resolve); // block reentry
timeoutHandle = setTimeout(() => {
if (resolveLoad) {
resolveLoad();
}
config.logger.error(`Timeout loading services for tenant ${requestTenant}`, requestTenant);
config.tenants[requestTenant] = new Tenant(requestTenant, { services: {} }, []); // empty tenant
timeoutHandle = null;
}, tenantLoadTimeoutMs);
// get the spec for the adapter to access the store where we can find services.json(s)
const tenantAdapter = await config.modules.getConfigAdapter(requestTenant);
const servicesRes = await tenantAdapter.read('services.json');
if (!servicesRes.ok) {
throw new Error('Could not read services.json');
}
const servicesConfig = await servicesRes.asJson() as IRawServicesConfig;
// specified domains for tenant
const tenantDomains = Object.entries(config.server.domainMap)
.filter(([,ten]) => ten === requestTenant)
.map(([dom,]) => dom);
// tenant's subdomain of main domain
tenantDomains.push(`${requestTenant}.${config.server.mainDomain}`);
config.tenants[requestTenant] = new Tenant(requestTenant, servicesConfig, tenantDomains);
await config.tenants[requestTenant].init();
config.logger.info(`Loaded tenant ${requestTenant} successfully`, requestTenant);
} catch (err) {
config.logger.error(`Failed to load services for tenant ${requestTenant}: ${err}`, requestTenant);
config.tenants[requestTenant] = new Tenant(requestTenant, { services: {} }, []); // empty tenant
throw err;
} finally {
if (timeoutHandle !== null) clearTimeout(timeoutHandle);
if (resolveLoad) resolveLoad(); // allow reentry
}
}
return config.tenants[requestTenant];
}
const tenantFromHostname = (hostname: string): string | null => {
const domainParts = hostname.split('.');
if (config.server.tenancy === 'single') {
return '';
} else if (config.server.domainMap && config.server.domainMap[hostname]) {
return config.server.domainMap[hostname];
} else {
const mainDomain: string[] = [];
mainDomain.unshift(domainParts.pop() as string);
mainDomain.unshift(domainParts.pop() as string);
if (mainDomain.join('.').toLowerCase() !== config.server.mainDomain.toLowerCase()) {
return null;
}
return domainParts.join('.');
}
}
export const handleIncomingRequest = async (msg: Message) => {
const originalMethod = msg.method;
let serviceName = '?' as string | undefined;
try {
const tenantName = tenantFromHostname(msg.getHeader('host') || 'none');
if (tenantName === null) return msg.setStatus(404, 'Not found');
const tenant = await getTenant(msg.url, tenantName || 'main');
msg.tenant = tenant.name;
msg = await tenant.attachUser(msg);
let messageFunction: MessageFunction;
[messageFunction, serviceName] = await tenant.getMessageFunctionByUrl(msg.url, Source.External);
config.logger.info(`${" ".repeat(msg.depth)}(Incoming) Request ${msg.method} ${msg.url}`, ...msg.loggerArgs(serviceName));
const msgOut = await messageFunction(msg.callDown());
msgOut.depth = msg.depth;
config.logger.info(`${" ".repeat(msg.depth - 1)}(Incoming) Respnse ${msg.method} ${msg.url}`, ...msg.loggerArgs(serviceName));
msgOut.callUp();
if (!msgOut.ok) {
config.logger.info(`${" ".repeat(msg.depth)}Respnse ${msgOut.status} ${await msgOut.data?.asString()} ${msg.method} ${msg.url}`, ...msgOut.loggerArgs(serviceName));
}
return msgOut;
} catch (err) {
let errStack = '';
if (err instanceof Error) {
errStack = ` at \n${err.stack || ''}`;
}
config.logger.warning(`request processing failed: ${err}${errStack}`, ...msg.loggerArgs(serviceName));
return originalMethod === 'OPTIONS'
? config.server.setServerCors(msg).setStatus(204)
: msg.setStatus(500, 'Server error');
}
};
export const handleOutgoingRequest = async (msg: Message, source = Source.Internal, context?: BaseContext) => {
const originalMethod = msg.method;
let tenantName: string | null = '';
let serviceName = '?' as string | undefined;
const loggerArgs = context ? contextLoggerArgs(context) : msg.loggerArgs(serviceName);
try {
if (msg.url.domain === '' || msg.url.domain === undefined) {
tenantName = msg.tenant;
if (msg.url.isRelative) throw new Error(`Cannot request a relative url ${msg.url}`);
} else {
tenantName = tenantFromHostname(msg.url.domain);
}
let msgOut: Message;
let tenant: Tenant;
if (tenantName !== null) {
try {
tenant = await getTenant(msg.url, tenantName || 'main');
if (tenant.isEmpty) tenantName = null;
} catch {
tenantName = null;
}
}
if (tenantName !== null) {
msg.tenant = tenantName;
let messageFunction: MessageFunction;
[messageFunction, serviceName] = await tenant!.getMessageFunctionByUrl(msg.url, source);
config.logger.info(`${" ".repeat(msg.depth)}Request ${msg.method} ${msg.url}`, ...loggerArgs);
msgOut = await messageFunction(msg.callDown());
msgOut.depth = msg.depth;
config.logger.info(`${" ".repeat(msgOut.depth - 1)}Respnse ${msg.method} ${msg.url}`, ...loggerArgs);
msgOut.callUp();
} else {
config.logger.info(`Request external ${msg.method} ${msg.url}`, ...loggerArgs);
let resp: Response;
let msgOut: Message;
if (config.requestExternal) {
try {
msgOut = await config.requestExternal(msg);
} catch (err) {
config.logger.error(`External request failed: ${err}`, ...loggerArgs);
msg.setStatus(500, `External request fail: ${err}`);
return msg;
}
} else {
try {
resp = await fetch(msg.toRequest());
} catch (err) {
config.logger.error(`External request failed: ${err}`, ...loggerArgs);
msg.setStatus(500, `External request fail: ${err}`);
return msg;
}
msgOut = Message.fromResponse(resp, msg.tenant);
}
msgOut.method = msg.method; // slightly pointless
msgOut.name = msg.name;
msg.setMetadataOn(msgOut);
if (msgOut.ok) {
config.logger.info(`Respnse external ${msg.method} ${msg.url}`, ...loggerArgs);
} else {
const body = msgOut.hasData() ? (await msgOut.data!.asString())?.substring(0, 200) : 'none';
config.logger.warning(`Respnse external ${msg.method} ${msg.url} error status ${msgOut.status} body ${body}`, ...loggerArgs);
}
// don't process by mime type on external requests
if (msgOut.data) msgOut.data.wasMimeHandled = true;
return msgOut;
}
if (!msgOut.ok) {
config.logger.info(` - Status ${msgOut.status} ${await msgOut.data?.asString()} ${msg.method} ${msg.url}`, ...loggerArgs);
}
return msgOut;
} catch (err) {
let errStack = '';
if (err instanceof Error) {
errStack = ` at \n${err.stack || ''}`;
}
config.logger.warning(`request processing failed: ${err}${errStack}`, ...loggerArgs);
return originalMethod === 'OPTIONS' && tenantName
? config.server.setServerCors(msg).setStatus(204)
: msg.setStatus(500, 'Server error');
}
}
export const handleOutgoingRequestWithPrivateServices = (basePath: string, privateServices: Record<string, IServiceConfig>, tenantName: string, context: BaseContext, prePost?: PrePost) =>
async (msg: Message) => {
if (msg.url.pathElements[0]?.startsWith('*')) {
const privateServiceName = msg.url.pathElements[0];
const serviceConfig = privateServices[privateServiceName.substring(1)];
if (!serviceConfig) return msg.setStatus(404, 'Not found');
// url received by private service on msg is made absolute, with basePath set correctly
// this enables the private service to know from where it was called
const newUrl = new Url(basePath).follow(msg.url);
newUrl.scheme = msg.url.scheme;
newUrl.domain = msg.url.domain;
newUrl.basePathElements = slashTrim(basePath).split('/').concat([ privateServiceName ]);
msg.url = newUrl;
const tenant = config.tenants[tenantName || 'main'];
const messageFunction = await tenant.getMessageFunctionForService(serviceConfig, Source.Internal, prePost);
const msgOut = await messageFunction(msg.callDown());
msgOut.depth = msg.depth;
msgOut.callUp();
return msgOut;
} else {
return handleOutgoingRequest(msg, Source.Internal, context);
}
}
//export const handleWsConnection = (