Http Kernel!
This commit is contained in:
@@ -1,35 +1,31 @@
|
||||
import {Service} from '../../../di/src/decorator/Service.ts'
|
||||
|
||||
export type CanonicalResolver = (key: string) => any
|
||||
import {Canonical} from './Canonical.ts'
|
||||
|
||||
export class DuplicateResolverKeyError extends Error {
|
||||
constructor(key: string) {
|
||||
super(`There is already a canonical resource with the scope ${key} registered.`)
|
||||
super(`There is already a canonical unit with the scope ${key} registered.`)
|
||||
}
|
||||
}
|
||||
|
||||
export class NoSuchCanonicalResolverKeyError extends Error {
|
||||
constructor(key: string) {
|
||||
super(`There is no such canonical unit with the scope ${key} registered.`)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Service()
|
||||
export class Canon {
|
||||
protected resources: { [key: string]: any } = {}
|
||||
protected resources: { [key: string]: Canonical<any> } = {}
|
||||
|
||||
get(key: string): any {
|
||||
const key_parts = key.split('::')
|
||||
let desc_value = this.resources
|
||||
key_parts.forEach(part => {
|
||||
if ( typeof desc_value === 'function' ) {
|
||||
desc_value = desc_value(part)
|
||||
} else {
|
||||
desc_value = desc_value[part]
|
||||
}
|
||||
})
|
||||
return desc_value
|
||||
resource<T>(key: string): Canonical<T> {
|
||||
if ( !this.resources[key] ) throw new NoSuchCanonicalResolverKeyError(key)
|
||||
return this.resources[key] as Canonical<T>
|
||||
}
|
||||
|
||||
register_resource(scope: string, resolver: CanonicalResolver) {
|
||||
if ( this.resources[scope] ) {
|
||||
throw new DuplicateResolverKeyError(scope)
|
||||
}
|
||||
|
||||
this.resources[scope] = resolver
|
||||
register_canonical(unit: Canonical<any>) {
|
||||
const key = unit.canonical_items
|
||||
if ( this.resources[key] ) throw new DuplicateResolverKeyError(key)
|
||||
this.resources[key] = unit
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,8 +28,7 @@ export class Canonical<T> extends LifecycleUnit {
|
||||
const def = await this._get_canonical_definition(entry.path)
|
||||
this._items[def.canonical_name] = await this.init_canonical_item(def)
|
||||
}
|
||||
|
||||
this.make(Canon).register_resource(this.canonical_items, (key: string) => this.get(key))
|
||||
this.make(Canon).register_canonical(this)
|
||||
}
|
||||
|
||||
public async init_canonical_item(definition: CanonicalDefinition): Promise<T> {
|
||||
|
||||
28
lib/src/unit/HttpKernel.ts
Normal file
28
lib/src/unit/HttpKernel.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import LifecycleUnit from "../lifecycle/Unit.ts";
|
||||
import {Unit} from "../lifecycle/decorators.ts";
|
||||
import Kernel from "../http/kernel/Kernel.ts";
|
||||
import PrepareRequest from "../http/kernel/module/PrepareRequest.ts";
|
||||
import SetSessionCookie from "../http/kernel/module/SetSessionCookie.ts";
|
||||
import Config from "./Config.ts";
|
||||
import SetDatonHeaders from "../http/kernel/module/SetDatonHeaders.ts";
|
||||
|
||||
@Unit()
|
||||
export default class HttpKernel extends LifecycleUnit {
|
||||
|
||||
constructor(
|
||||
protected readonly kernel: Kernel,
|
||||
protected readonly config: Config,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
public async up() {
|
||||
PrepareRequest.register(this.kernel)
|
||||
SetSessionCookie.register(this.kernel)
|
||||
|
||||
if ( this.config.get('server.powered_by.enable') ) {
|
||||
SetDatonHeaders.register(this.kernel)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { InstantiableCanonical } from './InstantiableCanonical.ts'
|
||||
import { CanonicalDefinition } from './Canonical.ts'
|
||||
import { Middleware } from '../http/Middleware.ts'
|
||||
import Middleware from '../http/Middleware.ts'
|
||||
import { Unit } from '../lifecycle/decorators.ts'
|
||||
|
||||
@Unit()
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import {Canonical} from './Canonical.ts'
|
||||
|
||||
export class RecursiveCanonical extends Canonical<any> {
|
||||
public get(key: string): any | undefined {
|
||||
public get(key: string, fallback?: any): any | undefined {
|
||||
const parts = key.split('.')
|
||||
let current_value = this._items
|
||||
for ( const part of parts ) {
|
||||
current_value = current_value?.[part]
|
||||
}
|
||||
return current_value
|
||||
return current_value ?? fallback
|
||||
}
|
||||
}
|
||||
|
||||
16
lib/src/unit/Routes.ts
Normal file
16
lib/src/unit/Routes.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import {Canonical, CanonicalDefinition} from './Canonical.ts'
|
||||
import {isRouterDefinition, RouterDefinition} from '../http/type/RouterDefinition.ts'
|
||||
|
||||
export default class Routes extends Canonical<RouterDefinition> {
|
||||
protected base_path = './app/http/routes'
|
||||
protected canonical_item = 'route_group'
|
||||
protected suffix = '.routes.ts'
|
||||
|
||||
public async init_canonical_item(def: CanonicalDefinition): Promise<RouterDefinition> {
|
||||
const item = await super.init_canonical_item(def)
|
||||
if ( !isRouterDefinition(item) ) {
|
||||
throw new TypeError(`Invalid routes definition: ${def.original_name}.`)
|
||||
}
|
||||
return item
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user