You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.0 KiB
67 lines
2.0 KiB
import {Singleton, Inject} from '../di'
|
|
import {UniversalPath, Collection} from '../util'
|
|
import {Unit} from '../lifecycle/Unit'
|
|
import {Logging} from './Logging'
|
|
import {Route} from '../http/routing/Route'
|
|
import {HTTPMethod} from '../http/lifecycle/Request'
|
|
import {ViewEngineFactory} from '../views/ViewEngineFactory'
|
|
|
|
/**
|
|
* Application unit that loads the various route files from `app/http/routes` and pre-compiles the route handlers.
|
|
*/
|
|
@Singleton()
|
|
export class Routing extends Unit {
|
|
@Inject()
|
|
protected readonly logging!: Logging
|
|
|
|
protected compiledRoutes: Collection<Route> = new Collection<Route>()
|
|
|
|
public async up(): Promise<void> {
|
|
this.app().registerFactory(new ViewEngineFactory())
|
|
|
|
for await ( const entry of this.path.walk() ) {
|
|
if ( !entry.endsWith('.routes.js') ) {
|
|
this.logging.debug(`Skipping routes file with invalid suffix: ${entry}`)
|
|
continue
|
|
}
|
|
|
|
this.logging.info(`Importing routes from: ${entry}`)
|
|
await import(entry)
|
|
}
|
|
|
|
this.logging.info('Compiling routes...')
|
|
this.compiledRoutes = new Collection<Route>(await Route.compile())
|
|
|
|
this.logging.info(`Compiled ${this.compiledRoutes.length} route(s).`)
|
|
this.compiledRoutes.each(route => {
|
|
this.logging.verbose(`${route}`)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Given an HTTPMethod and route path, return the Route instance that matches them,
|
|
* if one exists.
|
|
* @param method
|
|
* @param path
|
|
*/
|
|
public match(method: HTTPMethod, path: string): Route | undefined {
|
|
return this.compiledRoutes.firstWhere(route => {
|
|
return route.match(method, path)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Get the universal path to the root directory of the route definitions.
|
|
*/
|
|
public get path(): UniversalPath {
|
|
return this.app().appPath('http', 'routes')
|
|
}
|
|
|
|
/**
|
|
* Get the collection of compiled routes.
|
|
*/
|
|
public getCompiled(): Collection<Route> {
|
|
return this.compiledRoutes
|
|
}
|
|
}
|