RPCWebSocketServer
This commit is contained in:
@@ -2,7 +2,5 @@ import { Config } from "effect"
|
|||||||
|
|
||||||
|
|
||||||
export const httpPort = Config.number("HTTP_PORT").pipe(Config.withDefault(8080))
|
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 rpcHTTPRoot = Config.string("RPC_HTTP_ROOT").pipe(Config.withDefault("/rpc"))
|
||||||
export const rpcHTTPPlaygroundRoot = Config.string("RPC_HTTP_PLAYGROUND_ROOT").pipe(Config.withDefault("/rpc/playground"))
|
export const rpcHTTPPlaygroundRoot = Config.string("RPC_HTTP_PLAYGROUND_ROOT").pipe(Config.withDefault("/rpc/playground"))
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { ExpressApp } from "./ExpressApp"
|
|||||||
export class ExpressHTTPServer extends Context.Tag("ExpressHTTPServer")<ExpressHTTPServer, Server>() {}
|
export class ExpressHTTPServer extends Context.Tag("ExpressHTTPServer")<ExpressHTTPServer, Server>() {}
|
||||||
|
|
||||||
export module ExpressHTTPServer {
|
export module ExpressHTTPServer {
|
||||||
export const Live = Layer.scopedDiscard(Effect.acquireRelease(
|
export const Live = Layer.effect(ExpressHTTPServer, Effect.acquireRelease(
|
||||||
Effect.gen(function*() {
|
Effect.gen(function*() {
|
||||||
const runSync = yield* Effect.runtime().pipe(
|
const runSync = yield* Effect.runtime().pipe(
|
||||||
Effect.map(Runtime.runSync)
|
Effect.map(Runtime.runSync)
|
||||||
@@ -26,6 +26,5 @@ export module ExpressHTTPServer {
|
|||||||
server.close(() => resume(Effect.logInfo(`HTTP server closed`)))
|
server.close(() => resume(Effect.logInfo(`HTTP server closed`)))
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
)
|
))
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,27 @@
|
|||||||
import { applyWSSHandler } from "@trpc/server/adapters/ws"
|
import { applyWSSHandler } from "@trpc/server/adapters/ws"
|
||||||
import { Effect, Layer, Runtime } from "effect"
|
import { Context, Effect, Layer, Runtime } from "effect"
|
||||||
import { WebSocketServer } from "ws"
|
import ws from "ws"
|
||||||
import { websocketPort } from "../config"
|
import { ExpressHTTPServer } from "../express/ExpressHTTPServer"
|
||||||
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
|
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
|
||||||
import { RPCRouter } from "./RPCRouter"
|
import { RPCRouter } from "./RPCRouter"
|
||||||
|
|
||||||
|
|
||||||
|
export class RPCWebSocketServer extends Context.Tag("RPCWebSocketServer")<RPCWebSocketServer, {
|
||||||
|
wss: ws.Server
|
||||||
|
handler: ReturnType<typeof applyWSSHandler>
|
||||||
|
}>() {}
|
||||||
|
|
||||||
export module RPCWebSocketServer {
|
export module RPCWebSocketServer {
|
||||||
export const Live = Layer.scopedDiscard(Effect.acquireRelease(
|
export const Live = Layer.effect(RPCWebSocketServer, Effect.acquireRelease(
|
||||||
Effect.gen(function*() {
|
Effect.gen(function*() {
|
||||||
const runSync = yield* Effect.runtime().pipe(
|
const runSync = yield* Effect.runtime().pipe(
|
||||||
Effect.map(Runtime.runSync)
|
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({
|
const handler = applyWSSHandler({
|
||||||
wss,
|
wss,
|
||||||
@@ -24,7 +32,7 @@ export module RPCWebSocketServer {
|
|||||||
return { wss, handler }
|
return { wss, handler }
|
||||||
}),
|
}),
|
||||||
|
|
||||||
({ wss, handler }) => Effect.sync(() => {
|
({ wss, handler }) => Effect.gen(function*() {
|
||||||
handler.broadcastReconnectNotification()
|
handler.broadcastReconnectNotification()
|
||||||
wss.close()
|
wss.close()
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in New Issue
Block a user