Introduce async local storage for request access, more view globals, and improved welcome view
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-11-27 10:30:49 -06:00
parent b5eb407b55
commit 463076d182
8 changed files with 148 additions and 32 deletions

View File

@@ -19,6 +19,7 @@ import {Config} from './Config'
import {InjectRequestEventBusHTTPModule} from '../http/kernel/module/InjectRequestEventBusHTTPModule'
import {Routing} from './Routing'
import {EventBus} from '../event/EventBus'
import {RequestLocalStorage} from '../http/RequestLocalStorage'
/**
* Application unit that starts the HTTP/S server, creates Request and Response objects
@@ -41,6 +42,9 @@ export class HTTPServer extends Unit {
@Inject()
protected readonly bus!: EventBus
@Inject()
protected readonly requestLocalStorage!: RequestLocalStorage
/** The underlying native Node.js server. */
protected server?: Server
@@ -88,40 +92,42 @@ export class HTTPServer extends Unit {
return async (request: IncomingMessage, response: ServerResponse) => {
const extolloReq = new Request(request, response)
withTimeout(timeout, extolloReq.response.sent$.toPromise())
.onTime(() => {
this.logging.verbose(`Request lifecycle finished on time. (Path: ${extolloReq.path})`)
})
.late(() => {
if ( !extolloReq.bypassTimeout ) {
this.logging.warn(`Request lifecycle finished late, so an error response was returned! (Path: ${extolloReq.path})`)
}
})
.timeout(() => {
if ( extolloReq.bypassTimeout ) {
this.logging.info(`Request lifecycle has timed out, but bypassRequest was set. (Path: ${extolloReq.path})`)
return
await this.requestLocalStorage.run(extolloReq, async () => {
withTimeout(timeout, extolloReq.response.sent$.toPromise())
.onTime(() => {
this.logging.verbose(`Request lifecycle finished on time. (Path: ${extolloReq.path})`)
})
.late(() => {
if ( !extolloReq.bypassTimeout ) {
this.logging.warn(`Request lifecycle finished late, so an error response was returned! (Path: ${extolloReq.path})`)
}
})
.timeout(() => {
if ( extolloReq.bypassTimeout ) {
this.logging.info(`Request lifecycle has timed out, but bypassRequest was set. (Path: ${extolloReq.path})`)
return
}
this.logging.error(`Request lifecycle has timed out. Will send error response instead. (Path: ${extolloReq.path})`)
extolloReq.response.setStatus(HTTPStatus.REQUEST_TIMEOUT)
extolloReq.response.body = 'Sorry, your request timed out.'
extolloReq.response.send()
})
.run()
.catch(e => this.logging.error(e))
try {
await this.kernel.handle(extolloReq)
} catch (e) {
if ( e instanceof Error ) {
await error(e).write(extolloReq)
}
this.logging.error(`Request lifecycle has timed out. Will send error response instead. (Path: ${extolloReq.path})`)
extolloReq.response.setStatus(HTTPStatus.REQUEST_TIMEOUT)
extolloReq.response.body = 'Sorry, your request timed out.'
extolloReq.response.send()
})
.run()
.catch(e => this.logging.error(e))
try {
await this.kernel.handle(extolloReq)
} catch (e) {
if ( e instanceof Error ) {
await error(e).write(extolloReq)
await error(new ErrorWithContext('Unknown error occurred.', { e }))
}
await error(new ErrorWithContext('Unknown error occurred.', { e }))
}
await extolloReq.response.send()
await extolloReq.response.send()
})
}
}
}