@contember/engine-http 1.4.0-alpha.2 → 1.4.0-alpha.3
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/dist/src/MasterContainer.d.ts +14 -3
- package/dist/src/MasterContainer.d.ts.map +1 -1
- package/dist/src/MasterContainer.js +14 -4
- package/dist/src/MasterContainer.js.map +1 -1
- package/dist/src/application/application.d.ts +26 -10
- package/dist/src/application/application.d.ts.map +1 -1
- package/dist/src/application/application.js +53 -8
- package/dist/src/application/application.js.map +1 -1
- package/dist/src/config/configSchema.d.ts +1 -0
- package/dist/src/config/configSchema.d.ts.map +1 -1
- package/dist/src/config/configSchema.js +15 -0
- package/dist/src/config/configSchema.js.map +1 -1
- package/dist/src/config/configTemplate.d.ts.map +1 -1
- package/dist/src/config/configTemplate.js +1 -0
- package/dist/src/config/configTemplate.js.map +1 -1
- package/dist/src/content/ContentApiControllerFactory.d.ts.map +1 -1
- package/dist/src/content/ContentApiControllerFactory.js +3 -1
- package/dist/src/content/ContentApiControllerFactory.js.map +1 -1
- package/dist/src/content/ContentGraphQLContextFactory.d.ts +2 -5
- package/dist/src/content/ContentGraphQLContextFactory.d.ts.map +1 -1
- package/dist/src/content/ContentGraphQLContextFactory.js +1 -2
- package/dist/src/content/ContentGraphQLContextFactory.js.map +1 -1
- package/dist/src/content/ContentQueryHandlerFactory.d.ts.map +1 -1
- package/dist/src/content/ContentQueryHandlerFactory.js +1 -3
- package/dist/src/content/ContentQueryHandlerFactory.js.map +1 -1
- package/dist/src/graphql/execution.d.ts +1 -0
- package/dist/src/graphql/execution.d.ts.map +1 -1
- package/dist/src/graphql/execution.js +9 -11
- package/dist/src/graphql/execution.js.map +1 -1
- package/dist/src/graphql/state.d.ts +0 -5
- package/dist/src/graphql/state.d.ts.map +1 -1
- package/dist/src/graphql/state.js +0 -9
- package/dist/src/graphql/state.js.map +1 -1
- package/dist/src/project/ProjectContainer.d.ts +8 -24
- package/dist/src/project/ProjectContainer.d.ts.map +1 -1
- package/dist/src/project/ProjectContainer.js +18 -20
- package/dist/src/project/ProjectContainer.js.map +1 -1
- package/dist/src/system/ContentQueryExecutor.d.ts +9 -0
- package/dist/src/system/ContentQueryExecutor.d.ts.map +1 -0
- package/dist/src/system/ContentQueryExecutor.js +69 -0
- package/dist/src/system/ContentQueryExecutor.js.map +1 -0
- package/dist/src/system/SystemApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/system/SystemApiMiddlewareFactory.js +6 -2
- package/dist/src/system/SystemApiMiddlewareFactory.js.map +1 -1
- package/dist/src/system/SystemGraphQLContextFactory.d.ts +1 -4
- package/dist/src/system/SystemGraphQLContextFactory.d.ts.map +1 -1
- package/dist/src/system/SystemGraphQLContextFactory.js +2 -3
- package/dist/src/system/SystemGraphQLContextFactory.js.map +1 -1
- package/dist/src/system/SystemGraphQLHandlerFactory.d.ts +1 -5
- package/dist/src/system/SystemGraphQLHandlerFactory.d.ts.map +1 -1
- package/dist/src/system/SystemGraphQLHandlerFactory.js +1 -3
- package/dist/src/system/SystemGraphQLHandlerFactory.js.map +1 -1
- package/dist/src/tenant/TenantApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/src/tenant/TenantApiMiddlewareFactory.js +6 -2
- package/dist/src/tenant/TenantApiMiddlewareFactory.js.map +1 -1
- package/dist/src/tenant/TenantGraphQLContextFactory.d.ts +1 -4
- package/dist/src/tenant/TenantGraphQLContextFactory.d.ts.map +1 -1
- package/dist/src/tenant/TenantGraphQLContextFactory.js +1 -2
- package/dist/src/tenant/TenantGraphQLContextFactory.js.map +1 -1
- package/dist/src/tenant/TenantGraphQLHandlerFactory.d.ts +1 -3
- package/dist/src/tenant/TenantGraphQLHandlerFactory.d.ts.map +1 -1
- package/dist/src/tenant/TenantGraphQLHandlerFactory.js +1 -3
- package/dist/src/tenant/TenantGraphQLHandlerFactory.js.map +1 -1
- package/dist/src/transfer/ImportExecutor.d.ts +3 -1
- package/dist/src/transfer/ImportExecutor.d.ts.map +1 -1
- package/dist/src/transfer/ImportExecutor.js +4 -3
- package/dist/src/transfer/ImportExecutor.js.map +1 -1
- package/dist/src/tsconfig.tsbuildinfo +1 -1
- package/dist/src/utils/serverStartup.d.ts +1 -0
- package/dist/src/utils/serverStartup.d.ts.map +1 -1
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/package.json +34 -34
- package/src/MasterContainer.ts +24 -9
- package/src/application/application.ts +93 -23
- package/src/config/configSchema.ts +15 -0
- package/src/config/configTemplate.ts +1 -0
- package/src/content/ContentApiControllerFactory.ts +5 -1
- package/src/content/ContentGraphQLContextFactory.ts +2 -7
- package/src/content/ContentQueryHandlerFactory.ts +2 -10
- package/src/graphql/execution.ts +3 -3
- package/src/graphql/state.ts +0 -13
- package/src/project/ProjectContainer.ts +25 -35
- package/src/system/ContentQueryExecutor.ts +79 -0
- package/src/system/SystemApiMiddlewareFactory.ts +8 -2
- package/src/system/SystemGraphQLContextFactory.ts +2 -6
- package/src/system/SystemGraphQLHandlerFactory.ts +2 -16
- package/src/tenant/TenantApiMiddlewareFactory.ts +8 -3
- package/src/tenant/TenantGraphQLContextFactory.ts +1 -5
- package/src/tenant/TenantGraphQLHandlerFactory.ts +3 -11
- package/src/transfer/ImportExecutor.ts +4 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contember/engine-http",
|
|
3
|
-
"version": "1.4.0-alpha.
|
|
3
|
+
"version": "1.4.0-alpha.3",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"typings": "dist/src/index.d.ts",
|
|
@@ -12,47 +12,47 @@
|
|
|
12
12
|
"default": "./dist/src/index.js"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@apollographql/graphql-playground-html": "^1.6.
|
|
16
|
-
"@contember/config-loader": "1.4.0-alpha.
|
|
17
|
-
"@contember/database": "1.4.0-alpha.
|
|
18
|
-
"@contember/database-migrations": "1.4.0-alpha.
|
|
19
|
-
"@contember/dic": "1.4.0-alpha.
|
|
20
|
-
"@contember/engine-common": "1.4.0-alpha.
|
|
21
|
-
"@contember/engine-content-api": "1.4.0-alpha.
|
|
22
|
-
"@contember/engine-system-api": "1.4.0-alpha.
|
|
23
|
-
"@contember/engine-tenant-api": "1.4.0-alpha.
|
|
24
|
-
"@contember/graphql-utils": "1.4.0-alpha.
|
|
25
|
-
"@contember/logger": "1.4.0-alpha.
|
|
26
|
-
"@contember/schema": "1.4.0-alpha.
|
|
27
|
-
"@contember/schema-migrations": "1.4.0-alpha.
|
|
28
|
-
"@contember/schema-utils": "1.4.0-alpha.
|
|
29
|
-
"@contember/typesafe": "1.4.0-alpha.
|
|
15
|
+
"@apollographql/graphql-playground-html": "^1.6.29",
|
|
16
|
+
"@contember/config-loader": "1.4.0-alpha.3",
|
|
17
|
+
"@contember/database": "1.4.0-alpha.3",
|
|
18
|
+
"@contember/database-migrations": "1.4.0-alpha.3",
|
|
19
|
+
"@contember/dic": "1.4.0-alpha.3",
|
|
20
|
+
"@contember/engine-common": "1.4.0-alpha.3",
|
|
21
|
+
"@contember/engine-content-api": "1.4.0-alpha.3",
|
|
22
|
+
"@contember/engine-system-api": "1.4.0-alpha.3",
|
|
23
|
+
"@contember/engine-tenant-api": "1.4.0-alpha.3",
|
|
24
|
+
"@contember/graphql-utils": "1.4.0-alpha.3",
|
|
25
|
+
"@contember/logger": "1.4.0-alpha.3",
|
|
26
|
+
"@contember/schema": "1.4.0-alpha.3",
|
|
27
|
+
"@contember/schema-migrations": "1.4.0-alpha.3",
|
|
28
|
+
"@contember/schema-utils": "1.4.0-alpha.3",
|
|
29
|
+
"@contember/typesafe": "1.4.0-alpha.3",
|
|
30
30
|
"@graphql-tools/merge": "^9.0.0",
|
|
31
31
|
"@graphql-tools/schema": "^10.0.0",
|
|
32
|
-
"@koa/cors": "^
|
|
33
|
-
"@sentry/node": "^7.
|
|
32
|
+
"@koa/cors": "^4.0.0",
|
|
33
|
+
"@sentry/node": "^7.80.1",
|
|
34
34
|
"bcryptjs": "^2.4.3",
|
|
35
|
-
"koa": "^2.
|
|
36
|
-
"koa-bodyparser": "^4.
|
|
37
|
-
"koa-compress": "^5.1.
|
|
38
|
-
"lru-cache": "^
|
|
39
|
-
"path-to-regexp": "^6.2.
|
|
40
|
-
"uuid": "^
|
|
41
|
-
"ws": "^8.
|
|
35
|
+
"koa": "^2.14.2",
|
|
36
|
+
"koa-bodyparser": "^4.4.1",
|
|
37
|
+
"koa-compress": "^5.1.1",
|
|
38
|
+
"lru-cache": "^10.0.2",
|
|
39
|
+
"path-to-regexp": "^6.2.1",
|
|
40
|
+
"uuid": "^9.0.1",
|
|
41
|
+
"ws": "^8.14.2"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@types/bcryptjs": "^2.4.
|
|
45
|
-
"@types/koa": "^2.11
|
|
46
|
-
"@types/koa-bodyparser": "^4.
|
|
47
|
-
"@types/koa__cors": "^
|
|
44
|
+
"@types/bcryptjs": "^2.4.6",
|
|
45
|
+
"@types/koa": "^2.13.11",
|
|
46
|
+
"@types/koa-bodyparser": "^4.3.12",
|
|
47
|
+
"@types/koa__cors": "^4.0.3",
|
|
48
48
|
"@types/lru-cache": "^7.10.10",
|
|
49
|
-
"@types/node": "^
|
|
50
|
-
"@types/uuid": "^
|
|
51
|
-
"@types/ws": "^8.5.
|
|
52
|
-
"graphql": "^16.
|
|
49
|
+
"@types/node": "^20.9.0",
|
|
50
|
+
"@types/uuid": "^9.0.7",
|
|
51
|
+
"@types/ws": "^8.5.9",
|
|
52
|
+
"graphql": "^16.8.1"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"graphql": "
|
|
55
|
+
"graphql": ">= 14.6.0",
|
|
56
56
|
"pg": "^8.9.0"
|
|
57
57
|
}
|
|
58
58
|
}
|
package/src/MasterContainer.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { TenantConfigResolver } from './config/tenantConfigResolver'
|
|
|
11
11
|
import { ProjectGroupContainerFactory } from './projectGroup/ProjectGroupContainer'
|
|
12
12
|
import { ProjectGroupResolver, SingleProjectGroupResolver } from './projectGroup/ProjectGroupResolver'
|
|
13
13
|
import { Logger } from '@contember/logger'
|
|
14
|
-
import { ExecutionContainerFactory } from '@contember/engine-content-api'
|
|
14
|
+
import { ExecutionContainerFactory, GraphQlSchemaBuilderFactory, PermissionFactory } from '@contember/engine-content-api'
|
|
15
15
|
import { createProviders, Providers } from './providers'
|
|
16
16
|
import { TenantApiMiddlewareFactory, TenantGraphQLContextFactory, TenantGraphQLHandlerFactory } from './tenant'
|
|
17
17
|
import { SystemApiMiddlewareFactory, SystemGraphQLContextFactory, SystemGraphQLHandlerFactory } from './system'
|
|
@@ -34,6 +34,9 @@ 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'
|
|
38
|
+
import { ContentQueryExecutorImpl } from './system/ContentQueryExecutor'
|
|
39
|
+
import { DatabaseMetadataResolver } from '@contember/database'
|
|
37
40
|
|
|
38
41
|
export interface MasterContainer {
|
|
39
42
|
initializer: Initializer
|
|
@@ -93,10 +96,20 @@ export class MasterContainerFactory {
|
|
|
93
96
|
new TenantContainerFactory(providers))
|
|
94
97
|
.addService('modificationHandlerFactory', () =>
|
|
95
98
|
new ModificationHandlerFactory(ModificationHandlerFactory.defaultFactoryMap))
|
|
96
|
-
.addService('
|
|
97
|
-
new
|
|
98
|
-
.addService('
|
|
99
|
-
new
|
|
99
|
+
.addService('executionContainerFactory', ({ providers }) =>
|
|
100
|
+
new ExecutionContainerFactory(providers))
|
|
101
|
+
.addService('graphQlSchemaBuilderFactory', () =>
|
|
102
|
+
new GraphQlSchemaBuilderFactory())
|
|
103
|
+
.addService('contentQueryExecutor', ({ executionContainerFactory, graphQlSchemaBuilderFactory }) =>
|
|
104
|
+
new ContentQueryExecutorImpl(executionContainerFactory, graphQlSchemaBuilderFactory))
|
|
105
|
+
.addService('systemContainerFactory', ({ providers, modificationHandlerFactory, contentQueryExecutor }) =>
|
|
106
|
+
new SystemContainerFactory(providers, modificationHandlerFactory, contentQueryExecutor))
|
|
107
|
+
.addService('contentPermissionFactory', ({}) =>
|
|
108
|
+
new PermissionFactory())
|
|
109
|
+
.addService('databaseMetadataResolver', () =>
|
|
110
|
+
new DatabaseMetadataResolver())
|
|
111
|
+
.addService('projectContainerFactoryFactory', ({ plugins, providers, serverConfig, graphQlSchemaBuilderFactory, contentPermissionFactory, databaseMetadataResolver }) =>
|
|
112
|
+
new ProjectContainerFactoryFactory(plugins, providers, serverConfig, graphQlSchemaBuilderFactory, contentPermissionFactory, databaseMetadataResolver))
|
|
100
113
|
.addService('tenantGraphQLHandlerFactory', () =>
|
|
101
114
|
new TenantGraphQLHandlerFactory())
|
|
102
115
|
.addService('systemGraphQLHandlerFactory', ({ debugMode }) =>
|
|
@@ -111,8 +124,6 @@ export class MasterContainerFactory {
|
|
|
111
124
|
new SingleProjectGroupResolver(projectGroupContainer))
|
|
112
125
|
.addService('notModifiedChecker', () =>
|
|
113
126
|
new NotModifiedChecker())
|
|
114
|
-
.addService('executionContainerFactory', ({ providers }) =>
|
|
115
|
-
new ExecutionContainerFactory(providers))
|
|
116
127
|
.addService('contentGraphqlContextFactory', ({ providers, executionContainerFactory }) =>
|
|
117
128
|
new ContentGraphQLContextFactory(providers, executionContainerFactory))
|
|
118
129
|
.addService('contentQueryHandlerFactory', ({ debugMode }) =>
|
|
@@ -133,8 +144,8 @@ export class MasterContainerFactory {
|
|
|
133
144
|
new ContentSchemaTransferMappingFactory())
|
|
134
145
|
.addService('systemSchemaTransferMappingFactory', () =>
|
|
135
146
|
new SystemSchemaTransferMappingFactory())
|
|
136
|
-
.addService('importExecutor', ({ contentSchemaTransferMappingFactory, systemSchemaTransferMappingFactory }) =>
|
|
137
|
-
new ImportExecutor(contentSchemaTransferMappingFactory, systemSchemaTransferMappingFactory))
|
|
147
|
+
.addService('importExecutor', ({ contentSchemaTransferMappingFactory, systemSchemaTransferMappingFactory, databaseMetadataResolver }) =>
|
|
148
|
+
new ImportExecutor(contentSchemaTransferMappingFactory, systemSchemaTransferMappingFactory, databaseMetadataResolver))
|
|
138
149
|
.addService('exportExecutor', ({ contentSchemaTransferMappingFactory, systemSchemaTransferMappingFactory }) =>
|
|
139
150
|
new ExportExecutor(contentSchemaTransferMappingFactory, systemSchemaTransferMappingFactory))
|
|
140
151
|
.addService('importApiMiddlewareFactory', ({ projectGroupResolver, importExecutor }) =>
|
|
@@ -160,6 +171,10 @@ export class MasterContainerFactory {
|
|
|
160
171
|
it.addRoute('transfer', '/export', exportApiMiddlewareFactory.create())
|
|
161
172
|
it.addRoute('misc', '/playground', playgroundController)
|
|
162
173
|
it.addRoute('misc', '/', homepageController)
|
|
174
|
+
|
|
175
|
+
it.addInternalRoute('internal', '/health', () => {
|
|
176
|
+
return new HttpResponse(200, 'OK')
|
|
177
|
+
})
|
|
163
178
|
})
|
|
164
179
|
.addService('initializer', ({ projectGroupContainer }) =>
|
|
165
180
|
new Initializer(projectGroupContainer))
|
|
@@ -20,8 +20,11 @@ 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
|
|
|
26
|
+
private suppressAccessLog: boolean | RegExp
|
|
27
|
+
|
|
25
28
|
constructor(
|
|
26
29
|
private readonly projectGroupResolver: ProjectGroupResolver,
|
|
27
30
|
private readonly serverConfig: ServerConfig,
|
|
@@ -29,6 +32,8 @@ export class Application {
|
|
|
29
32
|
private readonly version: string | undefined,
|
|
30
33
|
private readonly logger: Logger,
|
|
31
34
|
) {
|
|
35
|
+
const suppressAccessLogRaw = serverConfig.http.suppressAccessLog
|
|
36
|
+
this.suppressAccessLog = suppressAccessLogRaw === true ? true : suppressAccessLogRaw ? new RegExp(suppressAccessLogRaw) : false
|
|
32
37
|
}
|
|
33
38
|
|
|
34
39
|
addMiddleware(middleware: KoaMiddleware<any>) {
|
|
@@ -43,6 +48,14 @@ export class Application {
|
|
|
43
48
|
})
|
|
44
49
|
}
|
|
45
50
|
|
|
51
|
+
public addInternalRoute(module: string, mask: string, controller: InternalHttpController): void {
|
|
52
|
+
this.internalRoutes.push({
|
|
53
|
+
module,
|
|
54
|
+
controller,
|
|
55
|
+
match: createRequestMatcher(mask),
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
46
59
|
public addWebsocketRoute(module: string, mask: string, controller: WebSocketController): void {
|
|
47
60
|
this.websocketRoutes.push({
|
|
48
61
|
module,
|
|
@@ -133,7 +146,6 @@ export class Application {
|
|
|
133
146
|
timer,
|
|
134
147
|
url,
|
|
135
148
|
request: req,
|
|
136
|
-
requestDebugMode: false,
|
|
137
149
|
authResult,
|
|
138
150
|
params: matchedRequest.params,
|
|
139
151
|
projectGroup: groupContainer,
|
|
@@ -164,6 +176,7 @@ export class Application {
|
|
|
164
176
|
requestLogger.debug('Websocket connection failed')
|
|
165
177
|
} finally {
|
|
166
178
|
sendTimer({
|
|
179
|
+
req,
|
|
167
180
|
requestDebugMode: false,
|
|
168
181
|
logger: requestLogger,
|
|
169
182
|
})
|
|
@@ -171,15 +184,21 @@ export class Application {
|
|
|
171
184
|
}
|
|
172
185
|
|
|
173
186
|
private async handleHttpRequest(ctx: KoaContext<{ module?: string; projectGroup?: string; project?: string }>) {
|
|
187
|
+
const matchedRequest = this.matchRequest(this.routes, ctx.request.URL)
|
|
188
|
+
if (!matchedRequest) {
|
|
189
|
+
const internalMatchedRequest = this.matchRequest(this.internalRoutes, ctx.request.URL)
|
|
190
|
+
if (internalMatchedRequest) {
|
|
191
|
+
return await this.handleInternalRequest(internalMatchedRequest, ctx)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
174
194
|
let httpContext: HttpContext | null = null
|
|
175
|
-
let requestLogger = this.
|
|
195
|
+
let requestLogger = this.createRequestLogger(ctx.req, ctx.request.body, matchedRequest?.module)
|
|
176
196
|
const { timer, send: sendTimer } = this.createTimer()
|
|
197
|
+
|
|
177
198
|
try {
|
|
178
|
-
const matchedRequest = this.matchRequest(this.routes, ctx.request.URL)
|
|
179
199
|
if (!matchedRequest) {
|
|
180
|
-
|
|
200
|
+
return this.sendHttpResponse(ctx, new HttpErrorResponse(404, 'Route not found'))
|
|
181
201
|
}
|
|
182
|
-
requestLogger = this.createRequestLogger(ctx.req, ctx.request.body, matchedRequest.module)
|
|
183
202
|
requestLogger.debug('Request processing started', {
|
|
184
203
|
body: ctx.request.body,
|
|
185
204
|
query: ctx.request.query,
|
|
@@ -230,6 +249,7 @@ export class Application {
|
|
|
230
249
|
}
|
|
231
250
|
} finally {
|
|
232
251
|
sendTimer({
|
|
252
|
+
req: ctx.req,
|
|
233
253
|
response: ctx.res,
|
|
234
254
|
body: ctx.response.body,
|
|
235
255
|
requestDebugMode: (httpContext as HttpContext | null)?.requestDebugMode ?? false,
|
|
@@ -239,6 +259,34 @@ export class Application {
|
|
|
239
259
|
}
|
|
240
260
|
}
|
|
241
261
|
|
|
262
|
+
|
|
263
|
+
private async handleInternalRequest(matchedRequest: MatchedRequest<InternalHttpController>, ctx: KoaContext<{ module?: string }>) {
|
|
264
|
+
try {
|
|
265
|
+
const response = await this.logger.scope(async logger => {
|
|
266
|
+
return await matchedRequest.controller({
|
|
267
|
+
koa: ctx,
|
|
268
|
+
body: ctx.request.body,
|
|
269
|
+
url: ctx.request.URL,
|
|
270
|
+
logger,
|
|
271
|
+
request: ctx.req,
|
|
272
|
+
response: ctx.res,
|
|
273
|
+
params: matchedRequest.params,
|
|
274
|
+
})
|
|
275
|
+
})
|
|
276
|
+
if (response) {
|
|
277
|
+
this.sendHttpResponse(ctx, response)
|
|
278
|
+
}
|
|
279
|
+
} catch (e) {
|
|
280
|
+
if (e instanceof HttpResponse) {
|
|
281
|
+
this.sendHttpResponse(ctx, e)
|
|
282
|
+
} else {
|
|
283
|
+
this.sendHttpResponse(ctx, new HttpErrorResponse(500, 'Internal server error'))
|
|
284
|
+
this.logger.error(e)
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
|
|
242
290
|
private sendHttpResponse(ctx: KoaContext<{}>, response: HttpResponse) {
|
|
243
291
|
if (response.contentType) {
|
|
244
292
|
ctx.set('Content-type', response.contentType)
|
|
@@ -264,11 +312,11 @@ export class Application {
|
|
|
264
312
|
.map(h => `${h}: ${headers[h]}`)
|
|
265
313
|
.join('\r\n') +
|
|
266
314
|
'\r\n\r\n' +
|
|
267
|
-
response.body ?? '',
|
|
315
|
+
(response.body ?? ''),
|
|
268
316
|
)
|
|
269
317
|
}
|
|
270
318
|
|
|
271
|
-
private createRequestLogger(request: IncomingMessage, body: any, module
|
|
319
|
+
private createRequestLogger(request: IncomingMessage, body: any, module?: string): Logger {
|
|
272
320
|
return this.logger.child({
|
|
273
321
|
method: request.method,
|
|
274
322
|
uri: request.url,
|
|
@@ -302,7 +350,7 @@ export class Application {
|
|
|
302
350
|
return res
|
|
303
351
|
}
|
|
304
352
|
|
|
305
|
-
const send = (ctx: { response?: ServerResponse; body?: unknown; requestDebugMode: boolean; logger: Logger }) => {
|
|
353
|
+
const send = (ctx: { req: IncomingMessage; response?: ServerResponse; body?: unknown; requestDebugMode: boolean; logger: Logger }) => {
|
|
306
354
|
if (ctx.response && !ctx.response.headersSent && (ctx.requestDebugMode || this.debugMode) && times.length) {
|
|
307
355
|
ctx.response.setHeader('server-timing', times.map(it => `${it.label};desc="${it.label} T+${it.start}";dur=${it.duration ?? -1}`).join(', '))
|
|
308
356
|
}
|
|
@@ -310,7 +358,10 @@ export class Application {
|
|
|
310
358
|
const emit = () => {
|
|
311
359
|
const total = new Date().getTime() - globalStart
|
|
312
360
|
const timeLabel = total > 500 ? 'TIME_SLOW' : 'TIME_OK'
|
|
313
|
-
|
|
361
|
+
const shouldSuppress = this.suppressAccessLog === true || !ctx.req.url || (this.suppressAccessLog !== false && this.suppressAccessLog.test(ctx.req.url))
|
|
362
|
+
|
|
363
|
+
const level = shouldSuppress ? 'debug' : 'info'
|
|
364
|
+
ctx.logger.log(level, !ctx.response ? 'Connection established' : ctx.response.statusCode < 400 ? `Request successful` : 'Request failed', {
|
|
314
365
|
status: ctx.response?.statusCode,
|
|
315
366
|
timeLabel: timeLabel,
|
|
316
367
|
totalTimeMs: total,
|
|
@@ -338,7 +389,7 @@ export class Application {
|
|
|
338
389
|
return { timer, send }
|
|
339
390
|
}
|
|
340
391
|
|
|
341
|
-
private matchRequest<C>(routes: Route<C>[], url: URL):
|
|
392
|
+
private matchRequest<C>(routes: Route<C>[], url: URL): MatchedRequest<C> | null {
|
|
342
393
|
for (const route of routes) {
|
|
343
394
|
const params = route.match({ url })
|
|
344
395
|
if (params !== null) {
|
|
@@ -354,31 +405,50 @@ type EventTime = { label: string; start: number; duration?: number }
|
|
|
354
405
|
|
|
355
406
|
export type Timer = <T>(event: string, cb: () => T) => T
|
|
356
407
|
|
|
357
|
-
export
|
|
408
|
+
export type MatchedRequest<C> = { params: Params; controller: C; module: string }
|
|
409
|
+
|
|
410
|
+
export type BaseRequestContext = {
|
|
358
411
|
logger: Logger
|
|
359
|
-
timer: Timer
|
|
360
412
|
request: IncomingMessage
|
|
361
413
|
url: URL
|
|
362
|
-
|
|
363
|
-
projectGroup: ProjectGroupContainer
|
|
364
|
-
authResult: AuthResult | null
|
|
365
414
|
params: Params
|
|
366
|
-
|
|
367
|
-
requestDebugMode: boolean
|
|
368
415
|
}
|
|
369
416
|
|
|
370
|
-
export
|
|
417
|
+
export type ApplicationContext =
|
|
418
|
+
& BaseRequestContext
|
|
419
|
+
& {
|
|
420
|
+
projectGroup: ProjectGroupContainer
|
|
421
|
+
authResult: AuthResult | null
|
|
422
|
+
timer: Timer
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export type BaseHttpRequestContext = {
|
|
371
426
|
koa: KoaContext<{}>
|
|
372
427
|
body: unknown
|
|
373
428
|
response: ServerResponse
|
|
374
429
|
}
|
|
375
430
|
|
|
376
|
-
export
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
431
|
+
export type HttpContext =
|
|
432
|
+
& ApplicationContext
|
|
433
|
+
& BaseHttpRequestContext
|
|
434
|
+
& {
|
|
435
|
+
requestDebugMode: boolean
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export type InternalHttpContext =
|
|
439
|
+
& BaseRequestContext
|
|
440
|
+
& BaseHttpRequestContext
|
|
441
|
+
|
|
442
|
+
export type WebSocketContext =
|
|
443
|
+
& ApplicationContext
|
|
444
|
+
& {
|
|
445
|
+
ws: WebSocket
|
|
446
|
+
abortSignal: AbortSignal
|
|
447
|
+
}
|
|
380
448
|
|
|
381
|
-
export type
|
|
449
|
+
export type HttpControllerResponse = Promise<HttpErrorResponse | undefined | void> | HttpErrorResponse | undefined | void
|
|
450
|
+
export type HttpController = (context: HttpContext) => HttpControllerResponse
|
|
451
|
+
export type InternalHttpController = (context: InternalHttpContext) => HttpControllerResponse
|
|
382
452
|
|
|
383
453
|
export type WebSocketController = (context: WebSocketContext) => void
|
|
384
454
|
|
|
@@ -87,6 +87,21 @@ export const serverConfigSchema = Typesafe.object({
|
|
|
87
87
|
port: Typesafe.number,
|
|
88
88
|
http: Typesafe.partial({
|
|
89
89
|
requestBodySize: Typesafe.string,
|
|
90
|
+
suppressAccessLog: (val: unknown) => {
|
|
91
|
+
if (!val) {
|
|
92
|
+
return undefined
|
|
93
|
+
}
|
|
94
|
+
if (val === 'true' || val === '1' || val === 'on') {
|
|
95
|
+
return true
|
|
96
|
+
}
|
|
97
|
+
if (val === 'false' || val === '0' || val === 'off') {
|
|
98
|
+
return false
|
|
99
|
+
}
|
|
100
|
+
if (typeof val === 'string') {
|
|
101
|
+
return val
|
|
102
|
+
}
|
|
103
|
+
Typesafe.fail([])
|
|
104
|
+
},
|
|
90
105
|
}),
|
|
91
106
|
contentApi: Typesafe.partial({
|
|
92
107
|
schemaCacheTtlSeconds: Typesafe.integer,
|
|
@@ -81,6 +81,7 @@ export const configTemplate: any = {
|
|
|
81
81
|
applicationWorker: '%?env.CONTEMBER_APPLICATION_WORKER::string%',
|
|
82
82
|
http: {
|
|
83
83
|
requestBodySize: '%?env.CONTEMBER_HTTP_REQUEST_BODY_SIZE::string%',
|
|
84
|
+
suppressAccessLog: '%?env.CONTEMBER_HTTP_SUPPRESS_ACCESS_LOG::string%',
|
|
84
85
|
},
|
|
85
86
|
contentApi: {
|
|
86
87
|
schemaCacheTtlSeconds: '%?env.CONTEMBER_CONTENT_API_SCHEMA_CACHE_TTL_SECONDS::number%',
|
|
@@ -6,6 +6,7 @@ import { NotModifiedChecker } from './NotModifiedChecker'
|
|
|
6
6
|
import { ContentGraphQLContextFactory } from './ContentGraphQLContextFactory'
|
|
7
7
|
import { ContentQueryHandler, ContentQueryHandlerFactory } from './ContentQueryHandlerFactory'
|
|
8
8
|
import { GraphQLSchema } from 'graphql'
|
|
9
|
+
import { GraphQLKoaState } from '../graphql'
|
|
9
10
|
|
|
10
11
|
const debugHeader = 'x-contember-debug'
|
|
11
12
|
|
|
@@ -103,6 +104,10 @@ export class ContentApiControllerFactory {
|
|
|
103
104
|
request: koa.request,
|
|
104
105
|
response: koa.response,
|
|
105
106
|
createContext: ({ operation }) => {
|
|
107
|
+
(koa.state as GraphQLKoaState).graphql = {
|
|
108
|
+
operationName: operation,
|
|
109
|
+
}
|
|
110
|
+
|
|
106
111
|
const connection = operation === 'query' ? projectContainer.readConnection : projectContainer.connection
|
|
107
112
|
const contentDatabase = connection.createClient(stage.schema, { module: 'content' })
|
|
108
113
|
|
|
@@ -114,7 +119,6 @@ export class ContentApiControllerFactory {
|
|
|
114
119
|
schemaDatabaseMetadata,
|
|
115
120
|
schema,
|
|
116
121
|
timer,
|
|
117
|
-
koaContext: koa,
|
|
118
122
|
requestDebug,
|
|
119
123
|
systemSchema: projectContainer.systemDatabaseContextFactory.schemaName,
|
|
120
124
|
stage,
|
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
import { Context, createAclVariables, ExecutionContainerFactory } from '@contember/engine-content-api'
|
|
2
2
|
import { Client, DatabaseMetadata } from '@contember/database'
|
|
3
3
|
import { Acl, Schema } from '@contember/schema'
|
|
4
|
-
|
|
5
4
|
import { AuthResult } from '../common'
|
|
6
|
-
import {
|
|
5
|
+
import { Timer } from '../application'
|
|
7
6
|
import { Providers } from '../providers'
|
|
8
|
-
import { GraphQLKoaState } from '../graphql'
|
|
9
7
|
import { ParsedMembership } from '@contember/schema-utils'
|
|
10
8
|
import { Stage } from '@contember/engine-system-api'
|
|
11
9
|
import { ProjectConfig } from '../project/config'
|
|
12
10
|
|
|
13
11
|
export type ExtendedGraphqlContext = Context & {
|
|
14
12
|
identityId: string
|
|
15
|
-
koaContext: KoaContext<GraphQLKoaState>
|
|
16
13
|
requestDebug: boolean
|
|
17
14
|
}
|
|
18
15
|
|
|
@@ -23,7 +20,7 @@ export class ContentGraphQLContextFactory {
|
|
|
23
20
|
) {
|
|
24
21
|
}
|
|
25
22
|
|
|
26
|
-
create({ db, schema, schemaDatabaseMetadata, authResult, memberships, permissions, timer,
|
|
23
|
+
create({ db, schema, schemaDatabaseMetadata, authResult, memberships, permissions, timer, requestDebug, systemSchema, stage, project }: {
|
|
27
24
|
db: Client
|
|
28
25
|
schema: Schema & { id: number }
|
|
29
26
|
schemaDatabaseMetadata: DatabaseMetadata
|
|
@@ -31,7 +28,6 @@ export class ContentGraphQLContextFactory {
|
|
|
31
28
|
authResult: AuthResult
|
|
32
29
|
memberships: readonly ParsedMembership[]
|
|
33
30
|
timer: Timer
|
|
34
|
-
koaContext: KoaContext<GraphQLKoaState>
|
|
35
31
|
requestDebug: boolean
|
|
36
32
|
systemSchema: string
|
|
37
33
|
stage: Stage
|
|
@@ -64,7 +60,6 @@ export class ContentGraphQLContextFactory {
|
|
|
64
60
|
identityId,
|
|
65
61
|
executionContainer,
|
|
66
62
|
timer,
|
|
67
|
-
koaContext,
|
|
68
63
|
requestDebug,
|
|
69
64
|
}
|
|
70
65
|
}
|
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import { GraphQLSchema } from 'graphql'
|
|
2
|
-
import {
|
|
3
|
-
createDbQueriesListener,
|
|
4
|
-
createGraphQLQueryHandler,
|
|
5
|
-
createGraphqlRequestInfoProviderListener,
|
|
6
|
-
GraphQLListener,
|
|
7
|
-
GraphQLQueryHandler,
|
|
8
|
-
} from '../graphql'
|
|
2
|
+
import { createDbQueriesListener, createGraphQLQueryHandler, GraphQLListener, GraphQLQueryHandler } from '../graphql'
|
|
9
3
|
import { ExtendedGraphqlContext } from './ContentGraphQLContextFactory'
|
|
10
4
|
|
|
11
5
|
export type ContentQueryHandler = GraphQLQueryHandler<ExtendedGraphqlContext>
|
|
@@ -14,9 +8,7 @@ export class ContentQueryHandlerFactory {
|
|
|
14
8
|
constructor(private readonly debug: boolean) {}
|
|
15
9
|
|
|
16
10
|
public create(graphQlSchema: GraphQLSchema): ContentQueryHandler {
|
|
17
|
-
const listeners: GraphQLListener<ExtendedGraphqlContext>[] = [
|
|
18
|
-
createGraphqlRequestInfoProviderListener(),
|
|
19
|
-
]
|
|
11
|
+
const listeners: GraphQLListener<ExtendedGraphqlContext>[] = []
|
|
20
12
|
listeners.push(createDbQueriesListener(context => context.db, this.debug))
|
|
21
13
|
|
|
22
14
|
return createGraphQLQueryHandler<ExtendedGraphqlContext>({
|
package/src/graphql/execution.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
validate,
|
|
11
11
|
validateSchema,
|
|
12
12
|
} from 'graphql'
|
|
13
|
-
import LRUCache from 'lru-cache'
|
|
13
|
+
import { LRUCache } from 'lru-cache'
|
|
14
14
|
import { createHash } from 'node:crypto'
|
|
15
15
|
import { Request, Response } from 'koa'
|
|
16
16
|
import { logger } from '@contember/logger'
|
|
@@ -65,7 +65,7 @@ export const createGraphQLQueryHandler = <Context>({
|
|
|
65
65
|
|
|
66
66
|
const now = Date.now()
|
|
67
67
|
if ((now - lastPrune) > pruneIntervalSeconds * 1000) {
|
|
68
|
-
documentCache.
|
|
68
|
+
documentCache.purgeStale()
|
|
69
69
|
lastPrune = now
|
|
70
70
|
}
|
|
71
71
|
const listenersQueue = [...listeners]
|
|
@@ -147,7 +147,7 @@ export const createGraphQLQueryHandler = <Context>({
|
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
|
|
150
|
-
const extractOriginalError = (e: Error): Error => {
|
|
150
|
+
export const extractOriginalError = (e: Error): Error => {
|
|
151
151
|
if (e instanceof GraphQLError && e.originalError) {
|
|
152
152
|
return extractOriginalError(e.originalError)
|
|
153
153
|
}
|
package/src/graphql/state.ts
CHANGED
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
import { GraphQLListener } from './execution'
|
|
2
|
-
import { KoaContext } from '../application'
|
|
3
|
-
|
|
4
|
-
export const createGraphqlRequestInfoProviderListener = (): GraphQLListener<{
|
|
5
|
-
koaContext: KoaContext<GraphQLKoaState>
|
|
6
|
-
}> => ({
|
|
7
|
-
onExecute: ({ context, operation }) => {
|
|
8
|
-
context.koaContext.state.graphql = {
|
|
9
|
-
operationName: operation,
|
|
10
|
-
}
|
|
11
|
-
},
|
|
12
|
-
})
|
|
13
|
-
|
|
14
1
|
export type GraphQLKoaState = {
|
|
15
2
|
graphql?: {
|
|
16
3
|
operationName: string | undefined
|