Models unit; session model; generalize session classes/interfaces

This commit is contained in:
garrettmills
2020-07-27 09:41:04 -05:00
parent 878de025d8
commit 25a37cf1a2
27 changed files with 331 additions and 37 deletions

View File

@@ -1,17 +1,29 @@
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 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()
}
@@ -23,6 +35,33 @@ export default class HttpKernel extends LifecycleUnit {
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))
}
}
}

View File

@@ -56,9 +56,5 @@ export default class Scaffolding extends LifecycleUnit {
public register_factories() {
this.logger.verbose('Adding the cache production factory to the container...')
this.injector.register_factory(new CacheFactory())
this.logger.verbose('Adding the session production factories to the container...')
this.injector.register_factory(new SessionFactory())
this.injector.register_factory(new SessionManagerFactory())
}
}

View File

@@ -0,0 +1,13 @@
import {Canonical, CanonicalDefinition} from './Canonical.ts'
import {InvalidCanonicalExportError} from './InstantiableCanonical.ts'
import {isStaticClass, StaticClass} from '../../../di/src/type/StaticClass.ts'
export class StaticCanonical<T, T2> extends Canonical<StaticClass<T, T2>> {
public async init_canonical_item(def: CanonicalDefinition): Promise<StaticClass<T, T2>> {
if ( isStaticClass(def.imported.default) ) {
return def.imported.default
}
throw new InvalidCanonicalExportError(def.original_name)
}
}