Add mounting for activated routes, route compiling, routing
This commit is contained in:
@@ -7,7 +7,8 @@ import {HTTPKernel} from "../http/kernel/HTTPKernel";
|
||||
import {PoweredByHeaderInjectionHTTPModule} from "../http/kernel/module/PoweredByHeaderInjectionHTTPModule";
|
||||
import {SetSessionCookieHTTPModule} from "../http/kernel/module/SetSessionCookieHTTPModule";
|
||||
import {InjectSessionHTTPModule} from "../http/kernel/module/InjectSessionHTTPModule";
|
||||
import {PersistSessionHTTPMiddleware} from "../http/kernel/module/PersistSessionHTTPMiddleware";
|
||||
import {PersistSessionHTTPModule} from "../http/kernel/module/PersistSessionHTTPModule";
|
||||
import {MountActivatedRouteHTTPModule} from "../http/kernel/module/MountActivatedRouteHTTPModule";
|
||||
|
||||
@Singleton()
|
||||
export class HTTPServer extends Unit {
|
||||
@@ -26,7 +27,8 @@ export class HTTPServer extends Unit {
|
||||
PoweredByHeaderInjectionHTTPModule.register(this.kernel)
|
||||
SetSessionCookieHTTPModule.register(this.kernel)
|
||||
InjectSessionHTTPModule.register(this.kernel)
|
||||
PersistSessionHTTPMiddleware.register(this.kernel)
|
||||
PersistSessionHTTPModule.register(this.kernel)
|
||||
MountActivatedRouteHTTPModule.register(this.kernel)
|
||||
|
||||
await new Promise<void>((res, rej) => {
|
||||
this.server = createServer(this.handler)
|
||||
|
||||
@@ -77,5 +77,7 @@ export class Logging {
|
||||
return e.stack.split(/\s+at\s+/)
|
||||
.slice(level)
|
||||
.map((x: string): string => x.trim().split(' (')[0].split('.')[0].split(':')[0])[0]
|
||||
.split('/')
|
||||
.reverse()[0]
|
||||
}
|
||||
}
|
||||
|
||||
44
src/service/Routing.ts
Normal file
44
src/service/Routing.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import {Singleton, Inject} from "@extollo/di"
|
||||
import {UniversalPath, Collection} from "@extollo/util"
|
||||
import {Unit} from "../lifecycle/Unit"
|
||||
import {Logging} from "./Logging"
|
||||
import {Route} from "../http/routing/Route";
|
||||
import {HTTPMethod} from "../http/lifecycle/Request";
|
||||
|
||||
@Singleton()
|
||||
export class Routing extends Unit {
|
||||
@Inject()
|
||||
protected readonly logging!: Logging
|
||||
|
||||
protected compiledRoutes: Collection<Route> = new Collection<Route>()
|
||||
|
||||
public async up() {
|
||||
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}`)
|
||||
})
|
||||
}
|
||||
|
||||
public match(method: HTTPMethod, path: string): Route | undefined {
|
||||
return this.compiledRoutes.firstWhere(route => {
|
||||
return route.match(method, path)
|
||||
})
|
||||
}
|
||||
|
||||
public get path(): UniversalPath {
|
||||
return this.app().appPath('http', 'routes')
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user