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.

90 lines
3.9 KiB

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'
import {Logging} from '../service/logging/Logging.ts'
import {Container} from '../../../di/src/Container.ts'
import MemorySessionFactory from '../http/session/MemorySessionFactory.ts'
import MemorySessionManagerFactory from '../http/session/MemorySessionManagerFactory.ts'
import ModelsUnit from '../../../orm/src/ModelsUnit.ts'
import {Model} from '../../../orm/src/model/Model.ts'
import {StaticClass} from '../../../di/src/type/StaticClass.ts'
import ModelSessionFactory from '../http/session/ModelSessionFactory.ts'
import ModelSessionManagerFactory from '../http/session/ModelSessionManagerFactory.ts'
import SessionInterface from '../http/session/SessionInterface.ts'
import InjectSession from '../http/kernel/module/InjectSession.ts'
import PersistSession from '../http/kernel/module/PersistSession.ts'
import MountActivatedRoute from '../http/kernel/module/MountActivatedRoute.ts'
import ApplyRouteHandlers from '../http/kernel/module/ApplyRouteHandlers.ts'
/**
* Lifecycle unit which bootstraps the HTTP kernel modules, and sets the session provider.
* @extends LifecycleUnit
*/
@Unit()
export default class HttpKernel extends LifecycleUnit {
constructor(
protected readonly kernel: Kernel,
protected readonly config: Config,
protected readonly logger: Logging,
protected readonly injector: Container,
protected readonly models: ModelsUnit,
) {
super()
}
public async up() {
this.determine_session_provider()
this.register_modules()
}
/**
* Determine the session provider from the config and register the appropriate factories.
*/
protected determine_session_provider() {
const driver = this.config.get('server.session.driver')
if ( driver === 'memory' ) {
this.logger.info('Adding the memory session production factories to the container...')
this.injector.register_factory(new MemorySessionFactory())
this.injector.register_factory(new MemorySessionManagerFactory())
} else if ( driver === 'database' ) {
const model_key = this.config.get('server.session.model')
if ( !model_key ) {
this.logger.error('Please specify the canonical model name to use for the HTTP session.')
throw new Error('Missing required config property: server.session.model')
}
const ModelClass: StaticClass<SessionInterface, typeof Model> | undefined = this.models.get(model_key)
if ( !ModelClass ) {
this.logger.error(`Unable to find HTTP session model with name: ${model_key}`)
throw new Error(`Unable to find HTTP session model with name: ${model_key}`)
}
this.logger.info('Adding the model session production factories to the container...')
this.injector.register_factory(new ModelSessionFactory(ModelClass))
this.injector.register_factory(new ModelSessionManagerFactory(ModelClass))
}
}
/**
* Register the default HTTP kernel modules with the kernel.
*/
protected register_modules() {
PrepareRequest.register(this.kernel)
SetSessionCookie.register(this.kernel)
InjectSession.register(this.kernel)
PersistSession.register(this.kernel)
MountActivatedRoute.register(this.kernel)
ApplyRouteHandlers.register(this.kernel)
if ( this.config.get('server.powered_by.enable') ) {
SetDatonHeaders.register(this.kernel)
}
}
}