|
|
|
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'
|
|
|
|
|
|
|
|
@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() {
|
|
|
|
PrepareRequest.register(this.kernel)
|
|
|
|
SetSessionCookie.register(this.kernel)
|
|
|
|
|
|
|
|
if ( this.config.get('server.powered_by.enable') ) {
|
|
|
|
SetDatonHeaders.register(this.kernel)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.determine_session_provider()
|
|
|
|
}
|
|
|
|
|
|
|
|
protected determine_session_provider() {
|
|
|
|
const driver = this.config.get('server.session.driver')
|
|
|
|
|
|
|
|
if ( driver === 'memory' ) {
|
|
|
|
this.logger.verbose('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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|