From 0d4881de1f95c18aeb60ecd140599d1671d4757c Mon Sep 17 00:00:00 2001 From: garrettmills Date: Wed, 29 Jul 2020 22:37:16 -0500 Subject: [PATCH] Logging & moment.js --- lib/src/external/std.ts | 1 + lib/src/http/kernel/Kernel.ts | 8 +++++++- lib/src/http/kernel/module/SetSessionCookie.ts | 6 +++++- lib/src/service/logging/Logger.ts | 5 ++++- lib/src/type/UUID.ts | 15 --------------- 5 files changed, 17 insertions(+), 18 deletions(-) delete mode 100755 lib/src/type/UUID.ts diff --git a/lib/src/external/std.ts b/lib/src/external/std.ts index 644a0dc..050fd77 100644 --- a/lib/src/external/std.ts +++ b/lib/src/external/std.ts @@ -3,3 +3,4 @@ export { config as dotenv } from 'https://deno.land/x/dotenv/mod.ts' export * as path from 'https://deno.land/std@0.53.0/path/mod.ts' export * as fs from 'https://deno.land/std@0.53.0/fs/mod.ts' export { generate as uuid } from 'https://deno.land/std/uuid/v4.ts' +export { moment } from 'https://deno.land/x/moment/moment.ts' diff --git a/lib/src/http/kernel/Kernel.ts b/lib/src/http/kernel/Kernel.ts index 456db26..6ce48f6 100644 --- a/lib/src/http/kernel/Kernel.ts +++ b/lib/src/http/kernel/Kernel.ts @@ -27,15 +27,20 @@ export default class Kernel extends AppClass { protected postflight: Collection = new Collection() public async handle(request: Request): Promise { + const logger = this.make(Logging) + for ( const module of this.preflight.toArray() ) { + logger.verbose(`Applying pre-flight HTTP kernel module: ${module.constructor.name}`) request = await module.apply(request) } if ( this.inflight ) { + logger.verbose(`Applying core HTTP kernel module: ${this.inflight.constructor.name}`) request = await this.inflight.apply(request) } for ( const module of this.postflight.toArray() ) { + logger.verbose(`Applying post-flight HTTP kernel module: ${module.constructor.name}`) request = await module.apply(request) } @@ -54,6 +59,7 @@ export default class Kernel extends AppClass { let found_index = this.preflight.find((mod: Module) => mod instanceof other) if ( typeof found_index !== 'undefined' ) { this.preflight = this.preflight.put(found_index, this.make(module)) + return this } else { found_index = this.postflight.find((mod: Module) => mod instanceof other) } @@ -75,6 +81,7 @@ export default class Kernel extends AppClass { let found_index = this.preflight.find((mod: Module) => mod instanceof other) if ( typeof found_index !== 'undefined' ) { this.preflight = this.preflight.put(found_index + 1, this.make(module)) + return this } else { found_index = this.postflight.find((mod: Module) => mod instanceof other) } @@ -82,7 +89,6 @@ export default class Kernel extends AppClass { if ( typeof found_index !== 'undefined' ) { this.postflight = this.postflight.put(found_index + 1, this.make(module)) } else { - console.log(this.preflight, this.postflight) throw new KernelModuleNotFoundError(other.name) } diff --git a/lib/src/http/kernel/module/SetSessionCookie.ts b/lib/src/http/kernel/module/SetSessionCookie.ts index 8543f85..8cccd0f 100644 --- a/lib/src/http/kernel/module/SetSessionCookie.ts +++ b/lib/src/http/kernel/module/SetSessionCookie.ts @@ -4,6 +4,7 @@ import {Request} from '../../Request.ts' import PrepareRequest from './PrepareRequest.ts' import Utility from '../../../service/utility/Utility.ts' import {Injectable} from '../../../../../di/src/decorator/Injection.ts' +import {Logging} from '../../../service/logging/Logging.ts' @Injectable() export default class SetSessionCookie extends Module { @@ -20,7 +21,10 @@ export default class SetSessionCookie extends Module { public async apply(request: Request): Promise { if ( !(await request.cookies.has('daton.session')) ) { - await request.cookies.set('daton.session', `${this.utility.uuid()}-${this.utility.uuid()}`) + const cookie = `${this.utility.uuid()}-${this.utility.uuid()}` + + this.make(Logging).verbose(`Starting session: ${cookie}`) + await request.cookies.set('daton.session', cookie) } return request } diff --git a/lib/src/service/logging/Logger.ts b/lib/src/service/logging/Logger.ts index b591735..ba9c34d 100644 --- a/lib/src/service/logging/Logger.ts +++ b/lib/src/service/logging/Logger.ts @@ -21,7 +21,10 @@ export default abstract class Logger { } protected format_date(date: Date): string { - return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}` + const hours = date.getHours() + const minutes = date.getMinutes() + const seconds = date.getSeconds() + return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()} ${hours > 9 ? hours : '0' + hours}:${minutes > 9 ? minutes : '0' + minutes}:${seconds > 9 ? seconds : '0' + seconds}` } protected level_display(level: LoggingLevel): string { diff --git a/lib/src/type/UUID.ts b/lib/src/type/UUID.ts deleted file mode 100755 index b697a5d..0000000 --- a/lib/src/type/UUID.ts +++ /dev/null @@ -1,15 +0,0 @@ -type UUID = string - -const isUUID = (possible: any): boolean => { - return typeof possible === 'string' && /[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}/.test(possible) -} - -const uuid = (): UUID => { - return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { - const r = Math.random() * 16 | 0 - const v = c == 'x' ? r : (r & 0x3 | 0x8) - return v.toString(16) - }) -} - -export { UUID, isUUID, uuid }