WebSocket work

This commit is contained in:
Julien Valverdé
2024-07-11 19:12:07 +02:00
parent 4f688719d7
commit 9ea6188f8b
6 changed files with 10 additions and 6 deletions

View File

@@ -0,0 +1,9 @@
import { Context, Layer } from "effect"
import express, { type Express } from "express"
export class ExpressApp extends Context.Tag("ExpressApp")<ExpressApp, Express>() {}
export module ExpressApp {
export const Live = Layer.sync(ExpressApp, () => express())
}

View File

@@ -0,0 +1,30 @@
import { Context, Effect, Layer, Runtime } from "effect"
import { Server } from "node:http"
import { httpPort } from "../config"
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 runSync = yield* Effect.runtime().pipe(
Effect.map(Runtime.runSync)
)
const app = yield* ExpressApp
const port = yield* httpPort
return app.listen(port, () => runSync(Effect.logInfo(`HTTP server listening on ${ port }`)))
}),
server => Effect.gen(function*() {
yield* Effect.logInfo(`HTTP server is closing. Waiting for existing connections to end...`)
yield* Effect.async(resume => {
server.close(() => resume(Effect.logInfo(`HTTP server closed`)))
})
}),
))
}