Add kernel modules and basic kernel handling
This commit is contained in:
parent
94add3d471
commit
fdcd80a43e
@ -50,6 +50,10 @@ export class HTTPCookieJar {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
has(name: string) {
|
||||||
|
return !!this.parsed[name]
|
||||||
|
}
|
||||||
|
|
||||||
clear(name: string, options?: HTTPCookieOptions) {
|
clear(name: string, options?: HTTPCookieOptions) {
|
||||||
if ( !options ) options = {}
|
if ( !options ) options = {}
|
||||||
options.expires = new Date(0)
|
options.expires = new Date(0)
|
||||||
@ -68,7 +72,9 @@ export class HTTPCookieJar {
|
|||||||
|
|
||||||
for ( const key in this.parsed ) {
|
for ( const key in this.parsed ) {
|
||||||
if ( !this.parsed.hasOwnProperty(key) ) continue
|
if ( !this.parsed.hasOwnProperty(key) ) continue
|
||||||
|
|
||||||
const cookie = this.parsed[key]
|
const cookie = this.parsed[key]
|
||||||
|
if ( cookie.exists ) continue
|
||||||
|
|
||||||
const parts = []
|
const parts = []
|
||||||
parts.push(`${key}=${encodeURIComponent(cookie.originalValue)}`)
|
parts.push(`${key}=${encodeURIComponent(cookie.originalValue)}`)
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
import {HTTPKernelModule} from "../HTTPKernelModule";
|
import {HTTPKernelModule} from "../HTTPKernelModule";
|
||||||
import {Request} from "../../lifecycle/Request";
|
import {Request} from "../../lifecycle/Request";
|
||||||
|
import {Injectable} from "@extollo/di"
|
||||||
import {HTTPKernel} from "../HTTPKernel";
|
import {HTTPKernel} from "../HTTPKernel";
|
||||||
|
|
||||||
export class PrepareRequestHTTPModule extends HTTPKernelModule {
|
@Injectable()
|
||||||
|
export class PoweredByHeaderInjectionHTTPModule extends HTTPKernelModule {
|
||||||
public static register(kernel: HTTPKernel) {
|
public static register(kernel: HTTPKernel) {
|
||||||
kernel.register(this).first()
|
kernel.register(this).after()
|
||||||
}
|
}
|
||||||
|
|
||||||
public async apply(request: Request) {
|
public async apply(request: Request) {
|
||||||
await request.prepare()
|
// FIXME make this configurable
|
||||||
|
request.response.setHeader('X-Powered-By', 'Extollo')
|
||||||
return request
|
return request
|
||||||
}
|
}
|
||||||
}
|
}
|
26
src/http/kernel/module/SetSessionCookieHTTPModule.ts
Normal file
26
src/http/kernel/module/SetSessionCookieHTTPModule.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import {HTTPKernelModule} from "../HTTPKernelModule";
|
||||||
|
import {Injectable, Inject} from "@extollo/di";
|
||||||
|
import {uuid_v4} from "@extollo/util";
|
||||||
|
import {HTTPKernel} from "../HTTPKernel";
|
||||||
|
import {Request} from "../../lifecycle/Request";
|
||||||
|
import {Logging} from "../../../service/Logging";
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class SetSessionCookieHTTPModule extends HTTPKernelModule {
|
||||||
|
@Inject()
|
||||||
|
protected readonly logging!: Logging
|
||||||
|
|
||||||
|
public static register(kernel: HTTPKernel) {
|
||||||
|
kernel.register(this).first() // TODO make this before inject session?
|
||||||
|
}
|
||||||
|
|
||||||
|
public async apply(request: Request) {
|
||||||
|
if ( !request.cookies.has('extollo.session') ) {
|
||||||
|
const session = `${uuid_v4()}-${uuid_v4()}`
|
||||||
|
|
||||||
|
this.logging.verbose(`Starting session: ${session}`)
|
||||||
|
request.cookies.set('extollo.session', session) // FIXME allow configuring this
|
||||||
|
}
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
}
|
@ -100,10 +100,6 @@ export class Request extends ScopedContainer {
|
|||||||
this.response = new Response(this, serverResponse)
|
this.response = new Response(this, serverResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async prepare() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public getHeader(name: string) {
|
public getHeader(name: string) {
|
||||||
return this.clientRequest.headers[name.toLowerCase()]
|
return this.clientRequest.headers[name.toLowerCase()]
|
||||||
}
|
}
|
||||||
|
@ -33,19 +33,19 @@ export class Application extends Container {
|
|||||||
|
|
||||||
public static getApplication(): Application {
|
public static getApplication(): Application {
|
||||||
const existing = <Container | undefined> globalRegistry.getGlobal('extollo/injector')
|
const existing = <Container | undefined> globalRegistry.getGlobal('extollo/injector')
|
||||||
if ( existing && !(existing instanceof Application) ) {
|
if ( existing instanceof Application ) {
|
||||||
|
return existing
|
||||||
|
} else if ( existing ) {
|
||||||
const app = new Application()
|
const app = new Application()
|
||||||
existing.cloneTo(app)
|
existing.cloneTo(app)
|
||||||
|
|
||||||
globalRegistry.setGlobal('extollo/injector', app)
|
globalRegistry.setGlobal('extollo/injector', app)
|
||||||
return app
|
return app
|
||||||
} else if ( !existing ) {
|
} else {
|
||||||
const app = new Application()
|
const app = new Application()
|
||||||
globalRegistry.setGlobal('extollo/injector', app)
|
globalRegistry.setGlobal('extollo/injector', app)
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
return existing
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected baseDir!: string
|
protected baseDir!: string
|
||||||
|
@ -3,17 +3,27 @@ import {Unit} from "../lifecycle/Unit";
|
|||||||
import {createServer, IncomingMessage, ServerResponse, Server} from "http";
|
import {createServer, IncomingMessage, ServerResponse, Server} from "http";
|
||||||
import {Logging} from "./Logging";
|
import {Logging} from "./Logging";
|
||||||
import {Request} from "../http/lifecycle/Request";
|
import {Request} from "../http/lifecycle/Request";
|
||||||
|
import {HTTPKernel} from "../http/kernel/HTTPKernel";
|
||||||
|
import {PoweredByHeaderInjectionHTTPModule} from "../http/kernel/module/PoweredByHeaderInjectionHTTPModule";
|
||||||
|
import {SetSessionCookieHTTPModule} from "../http/kernel/module/SetSessionCookieHTTPModule";
|
||||||
|
|
||||||
@Singleton()
|
@Singleton()
|
||||||
export class HTTPServer extends Unit {
|
export class HTTPServer extends Unit {
|
||||||
@Inject()
|
@Inject()
|
||||||
protected readonly logging!: Logging
|
protected readonly logging!: Logging
|
||||||
|
|
||||||
|
@Inject()
|
||||||
|
protected readonly kernel!: HTTPKernel
|
||||||
|
|
||||||
protected server?: Server
|
protected server?: Server
|
||||||
|
|
||||||
public async up() {
|
public async up() {
|
||||||
const port = 8000
|
const port = 8000
|
||||||
|
|
||||||
|
// TODO register these by config
|
||||||
|
PoweredByHeaderInjectionHTTPModule.register(this.kernel)
|
||||||
|
SetSessionCookieHTTPModule.register(this.kernel)
|
||||||
|
|
||||||
await new Promise<void>((res, rej) => {
|
await new Promise<void>((res, rej) => {
|
||||||
this.server = createServer(this.handler)
|
this.server = createServer(this.handler)
|
||||||
|
|
||||||
@ -37,10 +47,11 @@ export class HTTPServer extends Unit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public get handler() {
|
public get handler() {
|
||||||
return (request: IncomingMessage, response: ServerResponse) => {
|
return async (request: IncomingMessage, response: ServerResponse) => {
|
||||||
const extolloReq = new Request(request, response)
|
const extolloReq = new Request(request, response)
|
||||||
extolloReq.cookies.set('testing123', {foo: 'bar', bob: 123})
|
|
||||||
extolloReq.response.send('Hi, from Extollo!!')
|
await this.kernel.handle(extolloReq)
|
||||||
|
await extolloReq.response.send('Hi, from Extollo!!')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user