RPCWebSocketServer

This commit is contained in:
Julien Valverdé
2024-07-11 06:28:05 +02:00
parent b8210ddd66
commit 4f688719d7
3 changed files with 30 additions and 25 deletions

View File

@@ -2,7 +2,5 @@ import { Config } from "effect"
export const httpPort = Config.number("HTTP_PORT").pipe(Config.withDefault(8080))
export const websocketPort = Config.number("WEBSOCKET_PORT").pipe(Config.withDefault(3001))
export const rpcHTTPRoot = Config.string("RPC_HTTP_ROOT").pipe(Config.withDefault("/rpc"))
export const rpcHTTPPlaygroundRoot = Config.string("RPC_HTTP_PLAYGROUND_ROOT").pipe(Config.withDefault("/rpc/playground"))

View File

@@ -7,25 +7,24 @@ import { ExpressApp } from "./ExpressApp"
export class ExpressHTTPServer extends Context.Tag("ExpressHTTPServer")<ExpressHTTPServer, Server>() {}
export module ExpressHTTPServer {
export const Live = Layer.scopedDiscard(Effect.acquireRelease(
Effect.gen(function*() {
const runSync = yield* Effect.runtime().pipe(
Effect.map(Runtime.runSync)
)
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
const app = yield* ExpressApp
const port = yield* httpPort
return app.listen(port, () => runSync(Effect.logInfo(`HTTP server listening on ${ port }`)))
}),
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...`)
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`)))
})
}),
)
)
yield* Effect.async(resume => {
server.close(() => resume(Effect.logInfo(`HTTP server closed`)))
})
}),
))
}

View File

@@ -1,19 +1,27 @@
import { applyWSSHandler } from "@trpc/server/adapters/ws"
import { Effect, Layer, Runtime } from "effect"
import { WebSocketServer } from "ws"
import { websocketPort } from "../config"
import { Context, Effect, Layer, Runtime } from "effect"
import ws from "ws"
import { ExpressHTTPServer } from "../express/ExpressHTTPServer"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
import { RPCRouter } from "./RPCRouter"
export class RPCWebSocketServer extends Context.Tag("RPCWebSocketServer")<RPCWebSocketServer, {
wss: ws.Server
handler: ReturnType<typeof applyWSSHandler>
}>() {}
export module RPCWebSocketServer {
export const Live = Layer.scopedDiscard(Effect.acquireRelease(
export const Live = Layer.effect(RPCWebSocketServer, Effect.acquireRelease(
Effect.gen(function*() {
const runSync = yield* Effect.runtime().pipe(
Effect.map(Runtime.runSync)
)
const wss = new WebSocketServer({ port: yield* websocketPort })
// Extract this to its own service?
const wss = new ws.WebSocketServer({ server: yield* ExpressHTTPServer }, () =>
runSync(Effect.logInfo(`WebSocket RPC server started`))
)
const handler = applyWSSHandler({
wss,
@@ -24,7 +32,7 @@ export module RPCWebSocketServer {
return { wss, handler }
}),
({ wss, handler }) => Effect.sync(() => {
({ wss, handler }) => Effect.gen(function*() {
handler.broadcastReconnectNotification()
wss.close()
}),