32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { Context, Effect, Layer } from "effect"
|
|
import { Server } from "node:http"
|
|
import { ServerConfig } from "../ServerConfig"
|
|
import { ExpressApp } from "./ExpressApp"
|
|
|
|
|
|
export class ExpressHTTPServer extends Context.Tag("ExpressHTTPServer")<ExpressHTTPServer, Server>() {}
|
|
|
|
export module ExpressHTTPServer {
|
|
export const Live = Layer.effect(ExpressHTTPServer, Effect.acquireRelease(
|
|
Effect.gen(function*() {
|
|
const app = yield* ExpressApp
|
|
const port = yield* ServerConfig.httpPort
|
|
|
|
return yield* Effect.async<Server>(resume => {
|
|
const server = app.listen(port, () => resume(
|
|
Effect.succeed(server).pipe(
|
|
Effect.tap(Effect.logInfo(`HTTP server listening on ${ port }`))
|
|
)
|
|
))
|
|
})
|
|
}),
|
|
|
|
server => Effect.gen(function*() {
|
|
yield* Effect.logInfo("HTTP server is stopping. Waiting for existing connections to end...")
|
|
yield* Effect.async(resume => {
|
|
server.close(() => resume(Effect.logInfo("HTTP server closed")))
|
|
})
|
|
}),
|
|
))
|
|
}
|