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`)))
})

View File

@@ -7,17 +7,17 @@ import { TodoRepository, createDefaultTodos } from "./TodoRepository"
import { ExpressApp } from "./http/ExpressApp"
import { ExpressHTTPServer } from "./http/ExpressHTTPServer"
import { RPCPlaygroundRoute } from "./rpc/RPCPlaygroundRoute"
import { RPCRoute } from "./rpc/RPCRoute"
import { RPCRouter } from "./rpc/RPCRouter"
import { RPCServerRoute } from "./rpc/RPCServerRoute"
import { RPCWebSocketServer } from "./rpc/RPCWebSocketServer"
import { RPCWebSocketHandler } from "./rpc/RPCWebSocketHandler"
import { RPCProcedureBuilder } from "./rpc/procedures/RPCProcedureBuilder"
import { TRPCBuilder } from "./trpc/TRPCBuilder"
import { TRPCContextCreator } from "./trpc/TRPCContextCreator"
const ServerDev = ExpressHTTPServer.Live.pipe(
Layer.provide(RPCWebSocketServer.Live),
Layer.provide(RPCServerRoute.Live),
Layer.provide(RPCWebSocketHandler.Live),
Layer.provide(RPCRoute.Live),
Layer.provide(RPCPlaygroundRoute.Dev),
Layer.provide(RPCRouter.Live),
Layer.provide(RPCProcedureBuilder.Live),

View File

@@ -6,7 +6,7 @@ import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
import { RPCRouter } from "./RPCRouter"
export module RPCServerRoute {
export module RPCRoute {
export const Live = Layer.effectDiscard(Effect.gen(function*() {
const app = yield* ExpressApp

View File

@@ -0,0 +1,22 @@
import { applyWSSHandler } from "@trpc/server/adapters/ws"
import { Effect, Layer } from "effect"
import { WebSocketServer } from "../http/WebSocketServer"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
import { RPCRouter } from "./RPCRouter"
export module RPCWebSocketHandler {
export const Live = Layer.effectDiscard(Effect.acquireRelease(
Effect.gen(function*() {
return applyWSSHandler({
wss: yield* WebSocketServer,
router: yield* RPCRouter,
createContext: yield* TRPCContextCreator,
})
}),
handler => Effect.sync(() =>
handler.broadcastReconnectNotification()
),
))
}

View File

@@ -1,44 +0,0 @@
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"
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.effect(RPCWebSocketServer, Effect.acquireRelease(
Effect.gen(function*() {
const runSync = yield* Effect.runtime().pipe(
Effect.map(Runtime.runSync)
)
// 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,
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...`)
handler.broadcastReconnectNotification()
yield* Effect.async(resume => {
wss.close(() => resume(Effect.logInfo(`WebSocket server closed`)))
})
}),
))
}