RPCWebSocketServer

This commit is contained in:
Julien Valverdé
2024-07-09 02:15:18 +02:00
parent 3ac12bfe3e
commit c25c525d6f
8 changed files with 55 additions and 24 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -14,7 +14,8 @@
"@trpc/server": "^10.45.2", "@trpc/server": "^10.45.2",
"effect": "^3.4.7", "effect": "^3.4.7",
"express": "^4.19.2", "express": "^4.19.2",
"trpc-playground": "^1.0.4" "trpc-playground": "^1.0.4",
"ws": "^8.18.0"
}, },
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.21", "@types/express": "^4.17.21",

View File

@@ -0,0 +1,8 @@
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

@@ -1,20 +1,19 @@
import { Config, Effect, Layer } from "effect" import { Effect, Layer } from "effect"
import { httpPort } from "../config"
import { ExpressApp } from "./ExpressApp" import { ExpressApp } from "./ExpressApp"
export module ExpressHTTPServer { export module ExpressHTTPServer {
export const Live = Layer.scopedDiscard(Effect.gen(function*() { export const Live = Layer.scopedDiscard(Effect.acquireRelease(
const app = yield* ExpressApp Effect.gen(function*() {
const port = yield* Config.number("PORT").pipe(Config.withDefault(8080)) const app = yield* ExpressApp
const port = yield* httpPort
yield* Effect.acquireRelease( return app.listen(port, () => console.log(`HTTP server listening on ${ port }.`))
Effect.sync(() => }),
app.listen(port, () => console.log(`HTTP server listening on ${ port }.`))
),
server => Effect.sync(() => server => Effect.sync(() =>
server.close() server.close()
), ),
) ))
}))
} }

View File

@@ -1,8 +1,8 @@
import { Effect, Layer } from "effect" import { Effect, Layer } from "effect"
import { expressHandler } from "trpc-playground/handlers/express" import { expressHandler } from "trpc-playground/handlers/express"
import { rpcHTTPPlaygroundRoot, rpcHTTPRoot } from "../config"
import { ExpressApp } from "../express/ExpressApp" import { ExpressApp } from "../express/ExpressApp"
import { RPCRouter } from "./RPCRouter" import { RPCRouter } from "./RPCRouter"
import { rpcPlaygroundRoot, rpcRoot } from "./config"
export module RPCPlaygroundRoute { export module RPCPlaygroundRoute {
@@ -10,10 +10,10 @@ export module RPCPlaygroundRoute {
export const Dev = Layer.effectDiscard(Effect.gen(function*() { export const Dev = Layer.effectDiscard(Effect.gen(function*() {
const app = yield* ExpressApp const app = yield* ExpressApp
const playgroundEndpoint = yield* rpcPlaygroundRoot const playgroundEndpoint = yield* rpcHTTPPlaygroundRoot
const handler = expressHandler({ const handler = expressHandler({
trpcApiEndpoint: yield* rpcRoot, trpcApiEndpoint: yield* rpcHTTPRoot,
playgroundEndpoint, playgroundEndpoint,
router: yield* RPCRouter, router: yield* RPCRouter,
}) })

View File

@@ -1,16 +1,16 @@
import { createExpressMiddleware } from "@trpc/server/adapters/express" import { createExpressMiddleware } from "@trpc/server/adapters/express"
import { Effect, Layer } from "effect" import { Effect, Layer } from "effect"
import { rpcHTTPRoot } from "../config"
import { ExpressApp } from "../express/ExpressApp" import { ExpressApp } from "../express/ExpressApp"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator" import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
import { RPCRouter } from "./RPCRouter" import { RPCRouter } from "./RPCRouter"
import { rpcRoot } from "./config"
export module RPCServerRoute { export module RPCServerRoute {
export const Live = Layer.effectDiscard(Effect.gen(function*() { export const Live = Layer.effectDiscard(Effect.gen(function*() {
const app = yield* ExpressApp const app = yield* ExpressApp
app.use(yield* rpcRoot, app.use(yield* rpcHTTPRoot,
createExpressMiddleware({ createExpressMiddleware({
router: yield* RPCRouter, router: yield* RPCRouter,
createContext: yield* TRPCContextCreator, createContext: yield* TRPCContextCreator,

View File

@@ -0,0 +1,28 @@
import { applyWSSHandler } from "@trpc/server/adapters/ws"
import { Effect, Layer } from "effect"
import ws from "ws"
import { websocketPort } from "../config"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
import { RPCRouter } from "./RPCRouter"
export module RPCWebSocketServer {
export const Live = Layer.scopedDiscard(Effect.acquireRelease(
Effect.gen(function*() {
const wss = new ws.Server({ port: yield* websocketPort })
const handler = applyWSSHandler({
wss,
router: yield* RPCRouter,
createContext: yield* TRPCContextCreator,
})
return { wss, handler }
}),
({ wss, handler }) => Effect.sync(() => {
handler.broadcastReconnectNotification()
wss.close()
}),
))
}

View File

@@ -1,5 +0,0 @@
import { Config } from "effect"
export const rpcRoot = Config.string("RPC_ROOT").pipe(Config.withDefault("/rpc"))
export const rpcPlaygroundRoot = Config.string("RPC_PLAYGROUND_ROOT").pipe(Config.withDefault("/rpc/playground"))