parent
25a37cf1a2
commit
a6995c6a85
@ -0,0 +1,59 @@
|
|||||||
|
import Module from '../Module.ts'
|
||||||
|
import Kernel from '../Kernel.ts'
|
||||||
|
import {Request} from '../../Request.ts'
|
||||||
|
import SetSessionCookie from './SetSessionCookie.ts'
|
||||||
|
import SessionManager from '../../session/SessionManager.ts'
|
||||||
|
import {Logging} from '../../../service/logging/Logging.ts'
|
||||||
|
import {Injectable} from '../../../../../di/src/decorator/Injection.ts'
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export default class InjectSession extends Module {
|
||||||
|
public static register(kernel: Kernel) {
|
||||||
|
kernel.register(this).after(SetSessionCookie)
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
protected readonly sessions: SessionManager,
|
||||||
|
protected readonly logger: Logging,
|
||||||
|
) {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
|
public async apply(request: Request): Promise<Request> {
|
||||||
|
if ( request.session ) return request
|
||||||
|
|
||||||
|
let key: string | undefined
|
||||||
|
try {
|
||||||
|
const result = await request.cookies.get('daton.session')
|
||||||
|
key = result?.value
|
||||||
|
} catch (e) {
|
||||||
|
this.logger.error('Invalid Daton session cookie. The session will not be injected.')
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.logger.debug(`Cookie: ${await request.cookies.get_raw('daton.session')}`)
|
||||||
|
} catch (e2) {}
|
||||||
|
|
||||||
|
this.logger.debug(e)
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !key ) {
|
||||||
|
this.logger.warn(`No session key was found. Is the SetSessionCookie module registered?`)
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
|
||||||
|
const has_existing = await this.sessions.has_session(key)
|
||||||
|
if ( has_existing ) {
|
||||||
|
request.session = await this.sessions.get_session(key)
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
|
||||||
|
const new_session = await this.sessions.get_session()
|
||||||
|
this.logger.verbose(`Populating new session: ${key}`)
|
||||||
|
new_session.set_key(key)
|
||||||
|
await new_session.persist()
|
||||||
|
request.session = new_session
|
||||||
|
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
import Module from '../Module.ts'
|
||||||
|
import Kernel from '../Kernel.ts'
|
||||||
|
import {Request} from '../../Request.ts'
|
||||||
|
|
||||||
|
export default class PersistSession extends Module {
|
||||||
|
public static register(kernel: Kernel) {
|
||||||
|
kernel.register(this).last()
|
||||||
|
}
|
||||||
|
|
||||||
|
public async apply(request: Request): Promise<Request> {
|
||||||
|
await request.session.persist()
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
}
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue