Continue fleshing out HTTP request classes
This commit is contained in:
48
src/service/HTTPServer.ts
Normal file
48
src/service/HTTPServer.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
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";
|
||||
|
||||
@Singleton()
|
||||
export class HTTPServer extends Unit {
|
||||
@Inject()
|
||||
protected readonly logging!: Logging
|
||||
|
||||
protected server?: Server
|
||||
|
||||
public async up() {
|
||||
const port = 8000
|
||||
|
||||
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 (request: IncomingMessage, response: ServerResponse) => {
|
||||
const extolloReq = new Request(request)
|
||||
console.log(extolloReq)
|
||||
console.log(extolloReq.protocol)
|
||||
response.end('Hi, from Extollo!');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user