Logging & moment.js

This commit is contained in:
garrettmills 2020-07-29 22:37:16 -05:00
parent a04f083dbb
commit 0d4881de1f
No known key found for this signature in database
GPG Key ID: 6ACD58D6ADACFC6E
5 changed files with 17 additions and 18 deletions

View File

@ -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 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 * 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 { generate as uuid } from 'https://deno.land/std/uuid/v4.ts'
export { moment } from 'https://deno.land/x/moment/moment.ts'

View File

@ -27,15 +27,20 @@ export default class Kernel extends AppClass {
protected postflight: Collection<Module> = new Collection<Module>() protected postflight: Collection<Module> = new Collection<Module>()
public async handle(request: Request): Promise<Request> { public async handle(request: Request): Promise<Request> {
const logger = this.make(Logging)
for ( const module of this.preflight.toArray() ) { for ( const module of this.preflight.toArray() ) {
logger.verbose(`Applying pre-flight HTTP kernel module: ${module.constructor.name}`)
request = await module.apply(request) request = await module.apply(request)
} }
if ( this.inflight ) { if ( this.inflight ) {
logger.verbose(`Applying core HTTP kernel module: ${this.inflight.constructor.name}`)
request = await this.inflight.apply(request) request = await this.inflight.apply(request)
} }
for ( const module of this.postflight.toArray() ) { for ( const module of this.postflight.toArray() ) {
logger.verbose(`Applying post-flight HTTP kernel module: ${module.constructor.name}`)
request = await module.apply(request) 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) let found_index = this.preflight.find((mod: Module) => mod instanceof other)
if ( typeof found_index !== 'undefined' ) { if ( typeof found_index !== 'undefined' ) {
this.preflight = this.preflight.put(found_index, this.make(module)) this.preflight = this.preflight.put(found_index, this.make(module))
return this
} else { } else {
found_index = this.postflight.find((mod: Module) => mod instanceof other) 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) let found_index = this.preflight.find((mod: Module) => mod instanceof other)
if ( typeof found_index !== 'undefined' ) { if ( typeof found_index !== 'undefined' ) {
this.preflight = this.preflight.put(found_index + 1, this.make(module)) this.preflight = this.preflight.put(found_index + 1, this.make(module))
return this
} else { } else {
found_index = this.postflight.find((mod: Module) => mod instanceof other) 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' ) { if ( typeof found_index !== 'undefined' ) {
this.postflight = this.postflight.put(found_index + 1, this.make(module)) this.postflight = this.postflight.put(found_index + 1, this.make(module))
} else { } else {
console.log(this.preflight, this.postflight)
throw new KernelModuleNotFoundError(other.name) throw new KernelModuleNotFoundError(other.name)
} }

View File

@ -4,6 +4,7 @@ import {Request} from '../../Request.ts'
import PrepareRequest from './PrepareRequest.ts' import PrepareRequest from './PrepareRequest.ts'
import Utility from '../../../service/utility/Utility.ts' import Utility from '../../../service/utility/Utility.ts'
import {Injectable} from '../../../../../di/src/decorator/Injection.ts' import {Injectable} from '../../../../../di/src/decorator/Injection.ts'
import {Logging} from '../../../service/logging/Logging.ts'
@Injectable() @Injectable()
export default class SetSessionCookie extends Module { export default class SetSessionCookie extends Module {
@ -20,7 +21,10 @@ export default class SetSessionCookie extends Module {
public async apply(request: Request): Promise<Request> { public async apply(request: Request): Promise<Request> {
if ( !(await request.cookies.has('daton.session')) ) { 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 return request
} }

View File

@ -21,7 +21,10 @@ export default abstract class Logger {
} }
protected format_date(date: Date): string { 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 { protected level_display(level: LoggingLevel): string {

View File

@ -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 }