@contember/engine-http 1.3.6 → 1.3.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contember/engine-http",
3
- "version": "1.3.6",
3
+ "version": "1.3.7",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/src/index.js",
6
6
  "typings": "dist/src/index.d.ts",
@@ -13,20 +13,20 @@
13
13
  },
14
14
  "dependencies": {
15
15
  "@apollographql/graphql-playground-html": "^1.6.24",
16
- "@contember/config-loader": "1.3.6",
17
- "@contember/database": "1.3.6",
18
- "@contember/database-migrations": "1.3.6",
19
- "@contember/dic": "1.3.6",
20
- "@contember/engine-common": "1.3.6",
21
- "@contember/engine-content-api": "1.3.6",
22
- "@contember/engine-system-api": "1.3.6",
23
- "@contember/engine-tenant-api": "1.3.6",
24
- "@contember/graphql-utils": "1.3.6",
25
- "@contember/logger": "1.3.6",
26
- "@contember/schema": "1.3.6",
27
- "@contember/schema-migrations": "1.3.6",
28
- "@contember/schema-utils": "1.3.6",
29
- "@contember/typesafe": "1.3.6",
16
+ "@contember/config-loader": "1.3.7",
17
+ "@contember/database": "1.3.7",
18
+ "@contember/database-migrations": "1.3.7",
19
+ "@contember/dic": "1.3.7",
20
+ "@contember/engine-common": "1.3.7",
21
+ "@contember/engine-content-api": "1.3.7",
22
+ "@contember/engine-system-api": "1.3.7",
23
+ "@contember/engine-tenant-api": "1.3.7",
24
+ "@contember/graphql-utils": "1.3.7",
25
+ "@contember/logger": "1.3.7",
26
+ "@contember/schema": "1.3.7",
27
+ "@contember/schema-migrations": "1.3.7",
28
+ "@contember/schema-utils": "1.3.7",
29
+ "@contember/typesafe": "1.3.7",
30
30
  "@graphql-tools/merge": "^8.3.5",
31
31
  "@graphql-tools/schema": "^8.3.5",
32
32
  "@koa/cors": "^3.1.0",
@@ -34,6 +34,7 @@ import { homepageController, playgroundController } from './misc'
34
34
  import { Plugin } from './plugin/Plugin'
35
35
  import { Application } from './application/application'
36
36
  import { ApplicationWorkerManager } from './workers/ApplicationWorkerManager'
37
+ import { HttpResponse } from './common'
37
38
 
38
39
  export interface MasterContainer {
39
40
  initializer: Initializer
@@ -160,6 +161,10 @@ export class MasterContainerFactory {
160
161
  it.addRoute('transfer', '/export', exportApiMiddlewareFactory.create())
161
162
  it.addRoute('misc', '/playground', playgroundController)
162
163
  it.addRoute('misc', '/', homepageController)
164
+
165
+ it.addInternalRoute('internal', '/health', () => {
166
+ return new HttpResponse(200, 'OK')
167
+ })
163
168
  })
164
169
  .addService('initializer', ({ projectGroupContainer }) =>
165
170
  new Initializer(projectGroupContainer))
@@ -20,6 +20,7 @@ export class Application {
20
20
  private middlewares: KoaMiddleware<any>[] = []
21
21
 
22
22
  private routes: Route<HttpController>[] = []
23
+ private internalRoutes: Route<InternalHttpController>[] = []
23
24
  private websocketRoutes: Route<WebSocketController>[] = []
24
25
 
25
26
  constructor(
@@ -43,6 +44,14 @@ export class Application {
43
44
  })
44
45
  }
45
46
 
47
+ public addInternalRoute(module: string, mask: string, controller: InternalHttpController): void {
48
+ this.internalRoutes.push({
49
+ module,
50
+ controller,
51
+ match: createRequestMatcher(mask),
52
+ })
53
+ }
54
+
46
55
  public addWebsocketRoute(module: string, mask: string, controller: WebSocketController): void {
47
56
  this.websocketRoutes.push({
48
57
  module,
@@ -133,7 +142,6 @@ export class Application {
133
142
  timer,
134
143
  url,
135
144
  request: req,
136
- requestDebugMode: false,
137
145
  authResult,
138
146
  params: matchedRequest.params,
139
147
  projectGroup: groupContainer,
@@ -171,14 +179,22 @@ export class Application {
171
179
  }
172
180
 
173
181
  private async handleHttpRequest(ctx: KoaContext<{ module?: string; projectGroup?: string; project?: string }>) {
182
+ const matchedRequest = this.matchRequest(this.routes, ctx.request.URL)
183
+ if (!matchedRequest) {
184
+ const internalMatchedRequest = this.matchRequest(this.internalRoutes, ctx.request.URL)
185
+ if (internalMatchedRequest) {
186
+ return await this.handleInternalRequest(internalMatchedRequest, ctx)
187
+ }
188
+ }
174
189
  let httpContext: HttpContext | null = null
175
190
  let requestLogger = this.logger
176
191
  const { timer, send: sendTimer } = this.createTimer()
192
+
177
193
  try {
178
- const matchedRequest = this.matchRequest(this.routes, ctx.request.URL)
179
194
  if (!matchedRequest) {
180
- throw new HttpErrorResponse(404, 'Route not found')
195
+ return this.sendHttpResponse(ctx, new HttpErrorResponse(404, 'Route not found'))
181
196
  }
197
+
182
198
  requestLogger = this.createRequestLogger(ctx.req, ctx.request.body, matchedRequest.module)
183
199
  requestLogger.debug('Request processing started', {
184
200
  body: ctx.request.body,
@@ -239,6 +255,34 @@ export class Application {
239
255
  }
240
256
  }
241
257
 
258
+
259
+ private async handleInternalRequest(matchedRequest: MatchedRequest<InternalHttpController>, ctx: KoaContext<{ module?: string }>) {
260
+ try {
261
+ const response = await this.logger.scope(async logger => {
262
+ return await matchedRequest.controller({
263
+ koa: ctx,
264
+ body: ctx.request.body,
265
+ url: ctx.request.URL,
266
+ logger,
267
+ request: ctx.req,
268
+ response: ctx.res,
269
+ params: matchedRequest.params,
270
+ })
271
+ })
272
+ if (response) {
273
+ this.sendHttpResponse(ctx, response)
274
+ }
275
+ } catch (e) {
276
+ if (e instanceof HttpResponse) {
277
+ this.sendHttpResponse(ctx, e)
278
+ } else {
279
+ this.sendHttpResponse(ctx, new HttpErrorResponse(500, 'Internal server error'))
280
+ this.logger.error(e)
281
+ }
282
+ }
283
+ }
284
+
285
+
242
286
  private sendHttpResponse(ctx: KoaContext<{}>, response: HttpResponse) {
243
287
  if (response.contentType) {
244
288
  ctx.set('Content-type', response.contentType)
@@ -338,7 +382,7 @@ export class Application {
338
382
  return { timer, send }
339
383
  }
340
384
 
341
- private matchRequest<C>(routes: Route<C>[], url: URL): { params: Params; controller: C; module: string } | null {
385
+ private matchRequest<C>(routes: Route<C>[], url: URL): MatchedRequest<C> | null {
342
386
  for (const route of routes) {
343
387
  const params = route.match({ url })
344
388
  if (params !== null) {
@@ -354,31 +398,50 @@ type EventTime = { label: string; start: number; duration?: number }
354
398
 
355
399
  export type Timer = <T>(event: string, cb: () => T) => T
356
400
 
357
- export interface ApplicationContext {
401
+ export type MatchedRequest<C> = { params: Params; controller: C; module: string }
402
+
403
+ export type BaseRequestContext = {
358
404
  logger: Logger
359
- timer: Timer
360
405
  request: IncomingMessage
361
406
  url: URL
362
-
363
- projectGroup: ProjectGroupContainer
364
- authResult: AuthResult | null
365
407
  params: Params
366
-
367
- requestDebugMode: boolean
368
408
  }
369
409
 
370
- export interface HttpContext extends ApplicationContext {
410
+ export type ApplicationContext =
411
+ & BaseRequestContext
412
+ & {
413
+ projectGroup: ProjectGroupContainer
414
+ authResult: AuthResult | null
415
+ timer: Timer
416
+ }
417
+
418
+ export type BaseHttpRequestContext = {
371
419
  koa: KoaContext<{}>
372
420
  body: unknown
373
421
  response: ServerResponse
374
422
  }
375
423
 
376
- export interface WebSocketContext extends ApplicationContext {
377
- ws: WebSocket
378
- abortSignal: AbortSignal
379
- }
424
+ export type HttpContext =
425
+ & ApplicationContext
426
+ & BaseHttpRequestContext
427
+ & {
428
+ requestDebugMode: boolean
429
+ }
430
+
431
+ export type InternalHttpContext =
432
+ & BaseRequestContext
433
+ & BaseHttpRequestContext
434
+
435
+ export type WebSocketContext =
436
+ & ApplicationContext
437
+ & {
438
+ ws: WebSocket
439
+ abortSignal: AbortSignal
440
+ }
380
441
 
381
- export type HttpController = (context: HttpContext) => Promise<HttpErrorResponse | undefined | void> | HttpErrorResponse | undefined | void
442
+ export type HttpControllerResponse = Promise<HttpErrorResponse | undefined | void> | HttpErrorResponse | undefined | void
443
+ export type HttpController = (context: HttpContext) => HttpControllerResponse
444
+ export type InternalHttpController = (context: InternalHttpContext) => HttpControllerResponse
382
445
 
383
446
  export type WebSocketController = (context: WebSocketContext) => void
384
447