RPCWebSocketServer
This commit is contained in:
8
packages/server/src/config.ts
Normal file
8
packages/server/src/config.ts
Normal 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"))
|
||||
@@ -1,20 +1,19 @@
|
||||
import { Config, Effect, Layer } from "effect"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { httpPort } from "../config"
|
||||
import { ExpressApp } from "./ExpressApp"
|
||||
|
||||
|
||||
export module ExpressHTTPServer {
|
||||
export const Live = Layer.scopedDiscard(Effect.gen(function*() {
|
||||
const app = yield* ExpressApp
|
||||
const port = yield* Config.number("PORT").pipe(Config.withDefault(8080))
|
||||
export const Live = Layer.scopedDiscard(Effect.acquireRelease(
|
||||
Effect.gen(function*() {
|
||||
const app = yield* ExpressApp
|
||||
const port = yield* httpPort
|
||||
|
||||
yield* Effect.acquireRelease(
|
||||
Effect.sync(() =>
|
||||
app.listen(port, () => console.log(`HTTP server listening on ${ port }.`))
|
||||
),
|
||||
return app.listen(port, () => console.log(`HTTP server listening on ${ port }.`))
|
||||
}),
|
||||
|
||||
server => Effect.sync(() =>
|
||||
server.close()
|
||||
),
|
||||
)
|
||||
}))
|
||||
server => Effect.sync(() =>
|
||||
server.close()
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Effect, Layer } from "effect"
|
||||
import { expressHandler } from "trpc-playground/handlers/express"
|
||||
import { rpcHTTPPlaygroundRoot, rpcHTTPRoot } from "../config"
|
||||
import { ExpressApp } from "../express/ExpressApp"
|
||||
import { RPCRouter } from "./RPCRouter"
|
||||
import { rpcPlaygroundRoot, rpcRoot } from "./config"
|
||||
|
||||
|
||||
export module RPCPlaygroundRoute {
|
||||
@@ -10,10 +10,10 @@ export module RPCPlaygroundRoute {
|
||||
|
||||
export const Dev = Layer.effectDiscard(Effect.gen(function*() {
|
||||
const app = yield* ExpressApp
|
||||
const playgroundEndpoint = yield* rpcPlaygroundRoot
|
||||
const playgroundEndpoint = yield* rpcHTTPPlaygroundRoot
|
||||
|
||||
const handler = expressHandler({
|
||||
trpcApiEndpoint: yield* rpcRoot,
|
||||
trpcApiEndpoint: yield* rpcHTTPRoot,
|
||||
playgroundEndpoint,
|
||||
router: yield* RPCRouter,
|
||||
})
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { createExpressMiddleware } from "@trpc/server/adapters/express"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { rpcHTTPRoot } from "../config"
|
||||
import { ExpressApp } from "../express/ExpressApp"
|
||||
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
|
||||
import { RPCRouter } from "./RPCRouter"
|
||||
import { rpcRoot } from "./config"
|
||||
|
||||
|
||||
export module RPCServerRoute {
|
||||
export const Live = Layer.effectDiscard(Effect.gen(function*() {
|
||||
const app = yield* ExpressApp
|
||||
|
||||
app.use(yield* rpcRoot,
|
||||
app.use(yield* rpcHTTPRoot,
|
||||
createExpressMiddleware({
|
||||
router: yield* RPCRouter,
|
||||
createContext: yield* TRPCContextCreator,
|
||||
|
||||
28
packages/server/src/rpc/RPCWebSocketServer.ts
Normal file
28
packages/server/src/rpc/RPCWebSocketServer.ts
Normal 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()
|
||||
}),
|
||||
))
|
||||
}
|
||||
@@ -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"))
|
||||
Reference in New Issue
Block a user