|
|
@ -1,5 +1,5 @@
|
|
|
|
import {Inject, Singleton} from '../di'
|
|
|
|
import {Inject, Singleton} from '../di'
|
|
|
|
import {HTTPStatus, withTimeout} from '../util'
|
|
|
|
import {HTTPStatus, universalPath, UniversalPath, withTimeout} from '../util'
|
|
|
|
import {Unit} from '../lifecycle/Unit'
|
|
|
|
import {Unit} from '../lifecycle/Unit'
|
|
|
|
import {createServer, IncomingMessage, RequestListener, Server, ServerResponse} from 'http'
|
|
|
|
import {createServer, IncomingMessage, RequestListener, Server, ServerResponse} from 'http'
|
|
|
|
import {Logging} from './Logging'
|
|
|
|
import {Logging} from './Logging'
|
|
|
@ -17,6 +17,11 @@ import {ExecuteResolvedRoutePostflightHTTPModule} from '../http/kernel/module/Ex
|
|
|
|
import {ParseIncomingBodyHTTPModule} from '../http/kernel/module/ParseIncomingBodyHTTPModule'
|
|
|
|
import {ParseIncomingBodyHTTPModule} from '../http/kernel/module/ParseIncomingBodyHTTPModule'
|
|
|
|
import {Config} from './Config'
|
|
|
|
import {Config} from './Config'
|
|
|
|
import {InjectRequestEventBusHTTPModule} from '../http/kernel/module/InjectRequestEventBusHTTPModule'
|
|
|
|
import {InjectRequestEventBusHTTPModule} from '../http/kernel/module/InjectRequestEventBusHTTPModule'
|
|
|
|
|
|
|
|
import {Routing} from './Routing'
|
|
|
|
|
|
|
|
import {Route} from '../http/routing/Route'
|
|
|
|
|
|
|
|
import {staticServer} from '../http/servers/static'
|
|
|
|
|
|
|
|
import {EventBus} from '../event/EventBus'
|
|
|
|
|
|
|
|
import {PackageDiscovered} from '../support/PackageDiscovered'
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Application unit that starts the HTTP/S server, creates Request and Response objects
|
|
|
|
* Application unit that starts the HTTP/S server, creates Request and Response objects
|
|
|
@ -33,9 +38,39 @@ export class HTTPServer extends Unit {
|
|
|
|
@Inject()
|
|
|
|
@Inject()
|
|
|
|
protected readonly kernel!: HTTPKernel
|
|
|
|
protected readonly kernel!: HTTPKernel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
|
|
|
|
protected readonly routing!: Routing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
|
|
|
|
protected readonly bus!: EventBus
|
|
|
|
|
|
|
|
|
|
|
|
/** The underlying native Node.js server. */
|
|
|
|
/** The underlying native Node.js server. */
|
|
|
|
protected server?: Server
|
|
|
|
protected server?: Server
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Register an asset directory for the given package.
|
|
|
|
|
|
|
|
* This creates a static server route for the package with the
|
|
|
|
|
|
|
|
* configured vendor prefix.
|
|
|
|
|
|
|
|
* @param packageName
|
|
|
|
|
|
|
|
* @param basePath
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public async registerVendorAssets(packageName: string, basePath: UniversalPath): Promise<void> {
|
|
|
|
|
|
|
|
if ( this.config.get('server.builtIns.vendor.enabled', true) ) {
|
|
|
|
|
|
|
|
this.logging.debug(`Registering vendor assets route for package ${packageName} on ${basePath}...`)
|
|
|
|
|
|
|
|
await this.routing.registerRoutes(() => {
|
|
|
|
|
|
|
|
const prefix = this.config.get('server.builtIns.vendor.prefix', '/vendor')
|
|
|
|
|
|
|
|
Route.group(prefix, () => {
|
|
|
|
|
|
|
|
Route.group(packageName, () => {
|
|
|
|
|
|
|
|
Route.get('/**', staticServer({
|
|
|
|
|
|
|
|
basePath,
|
|
|
|
|
|
|
|
directoryListing: false,
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async up(): Promise<void> {
|
|
|
|
public async up(): Promise<void> {
|
|
|
|
const port = this.config.get('server.port', 8000)
|
|
|
|
const port = this.config.get('server.port', 8000)
|
|
|
|
|
|
|
|
|
|
|
@ -51,6 +86,8 @@ export class HTTPServer extends Unit {
|
|
|
|
ParseIncomingBodyHTTPModule.register(this.kernel)
|
|
|
|
ParseIncomingBodyHTTPModule.register(this.kernel)
|
|
|
|
InjectRequestEventBusHTTPModule.register(this.kernel)
|
|
|
|
InjectRequestEventBusHTTPModule.register(this.kernel)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await this.registerBuiltIns()
|
|
|
|
|
|
|
|
|
|
|
|
await new Promise<void>(res => {
|
|
|
|
await new Promise<void>(res => {
|
|
|
|
this.server = createServer(this.handler)
|
|
|
|
this.server = createServer(this.handler)
|
|
|
|
|
|
|
|
|
|
|
@ -111,4 +148,34 @@ export class HTTPServer extends Unit {
|
|
|
|
await extolloReq.response.send()
|
|
|
|
await extolloReq.response.send()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register built-in servers and routes. */
|
|
|
|
|
|
|
|
protected async registerBuiltIns(): Promise<void> {
|
|
|
|
|
|
|
|
const extolloAssets = universalPath(__dirname, '..', 'resources', 'assets')
|
|
|
|
|
|
|
|
await this.registerVendorAssets('@extollo/lib', extolloAssets)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.bus.subscribe(PackageDiscovered, async (event: PackageDiscovered) => {
|
|
|
|
|
|
|
|
if ( event.packageConfig?.extollo?.assets?.discover && event.packageConfig.name ) {
|
|
|
|
|
|
|
|
this.logging.debug(`Registering vendor assets for discovered package: ${event.packageConfig.name}`)
|
|
|
|
|
|
|
|
const basePath = event.packageConfig?.extollo?.assets?.basePath
|
|
|
|
|
|
|
|
if ( basePath && Array.isArray(basePath) ) {
|
|
|
|
|
|
|
|
const assetPath = event.packageJson.concat('..', ...basePath)
|
|
|
|
|
|
|
|
await this.registerVendorAssets(event.packageConfig.name, assetPath)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( this.config.get('server.builtIns.assets.enabled', true) ) {
|
|
|
|
|
|
|
|
const prefix = this.config.get('server.builtIns.assets.prefix', '/assets')
|
|
|
|
|
|
|
|
this.logging.debug(`Registering built-in assets server with prefix: ${prefix}`)
|
|
|
|
|
|
|
|
await this.routing.registerRoutes(() => {
|
|
|
|
|
|
|
|
Route.group(prefix, () => {
|
|
|
|
|
|
|
|
Route.get('/**', staticServer({
|
|
|
|
|
|
|
|
directoryListing: false,
|
|
|
|
|
|
|
|
basePath: ['resources', 'assets'],
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|