Add support for registering vendor asset routes
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import {Singleton, Inject} from '../di'
|
||||
import {UniversalPath, Collection, Pipe, universalPath} from '../util'
|
||||
import {Unit} from '../lifecycle/Unit'
|
||||
import {Inject, Singleton} from '../di'
|
||||
import {Awaitable, Collection, Pipe, universalPath, UniversalPath} from '../util'
|
||||
import {Unit, UnitStatus} from '../lifecycle/Unit'
|
||||
import {Logging} from './Logging'
|
||||
import {Route} from '../http/routing/Route'
|
||||
import {HTTPMethod} from '../http/lifecycle/Request'
|
||||
@@ -8,6 +8,8 @@ import {ViewEngineFactory} from '../views/ViewEngineFactory'
|
||||
import {ViewEngine} from '../views/ViewEngine'
|
||||
import {lib} from '../lib'
|
||||
import {Config} from './Config'
|
||||
import {EventBus} from '../event/EventBus'
|
||||
import {PackageDiscovered} from '../support/PackageDiscovered'
|
||||
|
||||
/**
|
||||
* Application unit that loads the various route files from `app/http/routes` and pre-compiles the route handlers.
|
||||
@@ -20,6 +22,9 @@ export class Routing extends Unit {
|
||||
@Inject()
|
||||
protected readonly config!: Config
|
||||
|
||||
@Inject()
|
||||
protected readonly bus!: EventBus
|
||||
|
||||
protected compiledRoutes: Collection<Route> = new Collection<Route>()
|
||||
|
||||
public async up(): Promise<void> {
|
||||
@@ -45,6 +50,44 @@ export class Routing extends Unit {
|
||||
this.compiledRoutes.each(route => {
|
||||
this.logging.verbose(`${route}`)
|
||||
})
|
||||
|
||||
this.bus.subscribe(PackageDiscovered, async (event: PackageDiscovered) => {
|
||||
const loadFrom = event.packageConfig?.extollo?.routes?.loadFrom
|
||||
if ( Array.isArray(loadFrom) ) {
|
||||
for ( const path of loadFrom ) {
|
||||
const loadFile = event.packageJson.concat('..', path)
|
||||
this.logging.debug(`Loading routes for package ${event.packageConfig.name} from ${loadFile}...`)
|
||||
await import(loadFile.toLocal)
|
||||
}
|
||||
|
||||
await this.recompile()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a callback that registers routes and recompiles the routing stack.
|
||||
* @param callback
|
||||
*/
|
||||
public async registerRoutes(callback: () => Awaitable<void>): Promise<void> {
|
||||
await callback()
|
||||
|
||||
if ( this.status === UnitStatus.Started ) {
|
||||
await this.recompile()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Recompile registered routes into resolved handler stacks.
|
||||
*/
|
||||
public async recompile(): Promise<void> {
|
||||
this.logging.debug('Recompiling routes...')
|
||||
this.compiledRoutes = this.compiledRoutes.concat(new Collection<Route>(await Route.compile()))
|
||||
|
||||
this.logging.debug(`Re-compiled ${this.compiledRoutes.length} route(s).`)
|
||||
this.compiledRoutes.each(route => {
|
||||
this.logging.verbose(`${route}`)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,7 +133,7 @@ export class Routing extends Unit {
|
||||
}
|
||||
|
||||
public getVendorPath(namespace: string, ...parts: string[]): UniversalPath {
|
||||
return this.getVendorBase().concat(encodeURIComponent(namespace), ...parts)
|
||||
return this.getVendorBase().concat(namespace, ...parts)
|
||||
}
|
||||
|
||||
public getVendorBase(): UniversalPath {
|
||||
|
||||
Reference in New Issue
Block a user