You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.8 KiB
59 lines
1.8 KiB
import {Singleton, Inject} from "@extollo/di"
|
|
import {Unit} from "../lifecycle/Unit";
|
|
import {createServer, IncomingMessage, ServerResponse, Server} from "http";
|
|
import {Logging} from "./Logging";
|
|
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()
|
|
export class HTTPServer extends Unit {
|
|
@Inject()
|
|
protected readonly logging!: Logging
|
|
|
|
@Inject()
|
|
protected readonly kernel!: HTTPKernel
|
|
|
|
protected server?: Server
|
|
|
|
public async up() {
|
|
const port = 8000
|
|
|
|
// TODO register these by config
|
|
PoweredByHeaderInjectionHTTPModule.register(this.kernel)
|
|
SetSessionCookieHTTPModule.register(this.kernel)
|
|
|
|
await new Promise<void>((res, rej) => {
|
|
this.server = createServer(this.handler)
|
|
|
|
this.server.listen(port, undefined, undefined, () => {
|
|
this.logging.success(`Server listening on port ${port}. Press ^C to stop.`)
|
|
})
|
|
|
|
process.on('SIGINT', res)
|
|
})
|
|
}
|
|
|
|
public async down() {
|
|
if ( this.server ) {
|
|
this.server.close(err => {
|
|
if ( err ) {
|
|
this.logging.error(`Error encountered while closing HTTP server: ${err.message}`)
|
|
this.logging.debug(err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
public get handler() {
|
|
return async (request: IncomingMessage, response: ServerResponse) => {
|
|
const extolloReq = new Request(request, response)
|
|
|
|
await this.kernel.handle(extolloReq)
|
|
await extolloReq.response.send('Hi, from Extollo!!')
|
|
}
|
|
}
|
|
|
|
}
|