RPC services refactoring
This commit is contained in:
@@ -20,8 +20,7 @@ export module ExpressHTTPServer {
|
|||||||
}),
|
}),
|
||||||
|
|
||||||
server => Effect.gen(function*() {
|
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 => {
|
yield* Effect.async(resume => {
|
||||||
server.close(() => resume(Effect.logInfo(`HTTP server closed`)))
|
server.close(() => resume(Effect.logInfo(`HTTP server closed`)))
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
import { applyWSSHandler } from "@trpc/server/adapters/ws"
|
|
||||||
import { Context, Effect, Layer, Runtime } from "effect"
|
import { Context, Effect, Layer, Runtime } from "effect"
|
||||||
import ws from "ws"
|
import ws from "ws"
|
||||||
import { ExpressHTTPServer } from "../http/ExpressHTTPServer"
|
import { ExpressHTTPServer } from "../http/ExpressHTTPServer"
|
||||||
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
|
|
||||||
|
|
||||||
|
|
||||||
export class WebSocketServer extends Context.Tag("RPCWebSocketServer")<WebSocketServer, {
|
export class WebSocketServer extends Context.Tag("RPCWebSocketServer")<WebSocketServer, ws.Server>() {}
|
||||||
wss: ws.Server
|
|
||||||
handler: ReturnType<typeof applyWSSHandler>
|
|
||||||
}>() {}
|
|
||||||
|
|
||||||
export module WebSocketServer {
|
export module WebSocketServer {
|
||||||
export const Live = Layer.effect(WebSocketServer, Effect.acquireRelease(
|
export const Live = Layer.effect(WebSocketServer, Effect.acquireRelease(
|
||||||
@@ -17,21 +12,13 @@ export module WebSocketServer {
|
|||||||
Effect.map(Runtime.runSync)
|
Effect.map(Runtime.runSync)
|
||||||
)
|
)
|
||||||
|
|
||||||
const wss = new ws.WebSocketServer({ server: yield* ExpressHTTPServer }, () =>
|
return new ws.WebSocketServer({ server: yield* ExpressHTTPServer }, () =>
|
||||||
runSync(Effect.logInfo(`WebSocket RPC server started`))
|
runSync(Effect.logInfo(`WebSocket server started`))
|
||||||
)
|
)
|
||||||
|
|
||||||
const handler = applyWSSHandler({
|
|
||||||
wss,
|
|
||||||
router: yield* RPCRouter,
|
|
||||||
createContext: yield* TRPCContextCreator,
|
|
||||||
})
|
|
||||||
|
|
||||||
return { wss, handler }
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
({ wss, handler }) => Effect.gen(function*() {
|
wss => Effect.gen(function*() {
|
||||||
yield* Effect.logInfo(`WebSocket server is closing. Waiting for existing connections to end...`)
|
yield* Effect.logInfo(`WebSocket server is stopping. Waiting for existing connections to end...`)
|
||||||
yield* Effect.async(resume => {
|
yield* Effect.async(resume => {
|
||||||
wss.close(() => resume(Effect.logInfo(`WebSocket server closed`)))
|
wss.close(() => resume(Effect.logInfo(`WebSocket server closed`)))
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -7,17 +7,17 @@ import { TodoRepository, createDefaultTodos } from "./TodoRepository"
|
|||||||
import { ExpressApp } from "./http/ExpressApp"
|
import { ExpressApp } from "./http/ExpressApp"
|
||||||
import { ExpressHTTPServer } from "./http/ExpressHTTPServer"
|
import { ExpressHTTPServer } from "./http/ExpressHTTPServer"
|
||||||
import { RPCPlaygroundRoute } from "./rpc/RPCPlaygroundRoute"
|
import { RPCPlaygroundRoute } from "./rpc/RPCPlaygroundRoute"
|
||||||
|
import { RPCRoute } from "./rpc/RPCRoute"
|
||||||
import { RPCRouter } from "./rpc/RPCRouter"
|
import { RPCRouter } from "./rpc/RPCRouter"
|
||||||
import { RPCServerRoute } from "./rpc/RPCServerRoute"
|
import { RPCWebSocketHandler } from "./rpc/RPCWebSocketHandler"
|
||||||
import { RPCWebSocketServer } from "./rpc/RPCWebSocketServer"
|
|
||||||
import { RPCProcedureBuilder } from "./rpc/procedures/RPCProcedureBuilder"
|
import { RPCProcedureBuilder } from "./rpc/procedures/RPCProcedureBuilder"
|
||||||
import { TRPCBuilder } from "./trpc/TRPCBuilder"
|
import { TRPCBuilder } from "./trpc/TRPCBuilder"
|
||||||
import { TRPCContextCreator } from "./trpc/TRPCContextCreator"
|
import { TRPCContextCreator } from "./trpc/TRPCContextCreator"
|
||||||
|
|
||||||
|
|
||||||
const ServerDev = ExpressHTTPServer.Live.pipe(
|
const ServerDev = ExpressHTTPServer.Live.pipe(
|
||||||
Layer.provide(RPCWebSocketServer.Live),
|
Layer.provide(RPCWebSocketHandler.Live),
|
||||||
Layer.provide(RPCServerRoute.Live),
|
Layer.provide(RPCRoute.Live),
|
||||||
Layer.provide(RPCPlaygroundRoute.Dev),
|
Layer.provide(RPCPlaygroundRoute.Dev),
|
||||||
Layer.provide(RPCRouter.Live),
|
Layer.provide(RPCRouter.Live),
|
||||||
Layer.provide(RPCProcedureBuilder.Live),
|
Layer.provide(RPCProcedureBuilder.Live),
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
|
|||||||
import { RPCRouter } from "./RPCRouter"
|
import { RPCRouter } from "./RPCRouter"
|
||||||
|
|
||||||
|
|
||||||
export module RPCServerRoute {
|
export module RPCRoute {
|
||||||
export const Live = Layer.effectDiscard(Effect.gen(function*() {
|
export const Live = Layer.effectDiscard(Effect.gen(function*() {
|
||||||
const app = yield* ExpressApp
|
const app = yield* ExpressApp
|
||||||
|
|
||||||
22
packages/server/src/rpc/RPCWebSocketHandler.ts
Normal file
22
packages/server/src/rpc/RPCWebSocketHandler.ts
Normal 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()
|
||||||
|
),
|
||||||
|
))
|
||||||
|
}
|
||||||
@@ -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`)))
|
|
||||||
})
|
|
||||||
}),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user