RPC services refactoring

This commit is contained in:
Julien Valverdé
2024-07-11 22:47:55 +02:00
parent 320ccb5bc5
commit d9df1e15ae
6 changed files with 33 additions and 69 deletions

View File

@@ -20,8 +20,7 @@ export module ExpressHTTPServer {
}),
server => Effect.gen(function*() {
yield* Effect.logInfo(`HTTP server is closing. Waiting for existing connections to end...`)
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`)))
})

View File

@@ -1,14 +1,9 @@
import { applyWSSHandler } from "@trpc/server/adapters/ws"
import { Context, Effect, Layer, Runtime } from "effect"
import ws from "ws"
import { ExpressHTTPServer } from "../http/ExpressHTTPServer"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
export class WebSocketServer extends Context.Tag("RPCWebSocketServer")<WebSocketServer, {
wss: ws.Server
handler: ReturnType<typeof applyWSSHandler>
}>() {}
export class WebSocketServer extends Context.Tag("RPCWebSocketServer")<WebSocketServer, ws.Server>() {}
export module WebSocketServer {
export const Live = Layer.effect(WebSocketServer, Effect.acquireRelease(
@@ -17,21 +12,13 @@ export module WebSocketServer {
Effect.map(Runtime.runSync)
)
const wss = new ws.WebSocketServer({ server: yield* ExpressHTTPServer }, () =>
runSync(Effect.logInfo(`WebSocket RPC server started`))
return new ws.WebSocketServer({ server: yield* ExpressHTTPServer }, () =>
runSync(Effect.logInfo(`WebSocket server started`))
)
const handler = applyWSSHandler({
wss,
router: yield* RPCRouter,
createContext: yield* TRPCContextCreator,
})
return { wss, handler }
}),
({ wss, handler }) => Effect.gen(function*() {
yield* Effect.logInfo(`WebSocket server is closing. Waiting for existing connections to end...`)
wss => Effect.gen(function*() {
yield* Effect.logInfo(`WebSocket server is stopping. Waiting for existing connections to end...`)
yield* Effect.async(resume => {
wss.close(() => resume(Effect.logInfo(`WebSocket server closed`)))
})