Create InjectSession & PersistSession modules; update session model

This commit is contained in:
garrettmills
2020-07-27 19:48:44 -05:00
parent 25a37cf1a2
commit a6995c6a85
14 changed files with 149 additions and 14 deletions

View File

@@ -15,6 +15,8 @@ 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'
@Unit()
export default class HttpKernel extends LifecycleUnit {
@@ -29,21 +31,23 @@ export default class HttpKernel extends LifecycleUnit {
}
public async up() {
this.determine_session_provider()
PrepareRequest.register(this.kernel)
SetSessionCookie.register(this.kernel)
InjectSession.register(this.kernel)
PersistSession.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.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' ) {

View File

@@ -0,0 +1,32 @@
import LifecycleUnit from '../lifecycle/Unit.ts'
import {Unit} from '../lifecycle/decorators.ts'
import Kernel from '../http/kernel/Kernel.ts'
import {Logging} from '../service/logging/Logging.ts'
import {serve} from '../external/http.ts'
import {Request} from '../http/Request.ts'
@Unit()
export default class HttpServer extends LifecycleUnit {
protected _server: any // TODO replace with more specific type
constructor(
protected readonly kernel: Kernel,
protected readonly logger: Logging,
) {
super()
}
public async up() {
this._server = serve({ port: 8000 })
this.logger.success(`HTTP/S server listening on port 8000!`)
for await ( const native_request of this._server ) {
let req: Request = this.make(Request, native_request)
req = await this.kernel.handle(req)
req.response.body = req.session.get_key()
req.response.send()
}
}
}