Express work

This commit is contained in:
Julien Valverdé
2024-07-11 03:49:14 +02:00
parent a04cd49d03
commit b8210ddd66
5 changed files with 31 additions and 16 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -2,7 +2,7 @@ import { Context, Layer } from "effect"
import express, { type Express } from "express"
export class ExpressApp extends Context.Tag("Express")<ExpressApp, Express>() {}
export class ExpressApp extends Context.Tag("ExpressApp")<ExpressApp, Express>() {}
export module ExpressApp {
export const Live = Layer.sync(ExpressApp, () => express())

View File

@@ -1,19 +1,31 @@
import { Effect, Layer } from "effect"
import { Context, Effect, Layer, Runtime } from "effect"
import { Server } from "node:http"
import { httpPort } from "../config"
import { ExpressApp } from "./ExpressApp"
export class ExpressHTTPServer extends Context.Tag("ExpressHTTPServer")<ExpressHTTPServer, Server>() {}
export module ExpressHTTPServer {
export const Live = Layer.scopedDiscard(Effect.acquireRelease(
Effect.gen(function*() {
const app = yield* ExpressApp
const port = yield* httpPort
Effect.gen(function*() {
const runSync = yield* Effect.runtime().pipe(
Effect.map(Runtime.runSync)
)
return app.listen(port, () => console.log(`HTTP server listening on ${ port }.`))
}),
const app = yield* ExpressApp
const port = yield* httpPort
server => Effect.sync(() =>
server.close()
),
))
return app.listen(port, () => runSync(Effect.logInfo(`HTTP server listening on ${ port }`)))
}),
server => Effect.gen(function*() {
yield* Effect.logInfo(`HTTP server is closing. Waiting for existing connections to end...`)
yield* Effect.async(resume => {
server.close(() => resume(Effect.logInfo(`HTTP server closed`)))
})
}),
)
)
}

View File

@@ -1,6 +1,6 @@
import { applyWSSHandler } from "@trpc/server/adapters/ws"
import { Effect, Layer } from "effect"
import ws from "ws"
import { Effect, Layer, Runtime } from "effect"
import { WebSocketServer } from "ws"
import { websocketPort } from "../config"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
import { RPCRouter } from "./RPCRouter"
@@ -9,7 +9,11 @@ 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 runSync = yield* Effect.runtime().pipe(
Effect.map(Runtime.runSync)
)
const wss = new WebSocketServer({ port: yield* websocketPort })
const handler = applyWSSHandler({
wss,

View File

@@ -20,8 +20,7 @@
"mobx-react-lite": "^4.0.7",
"primereact": "^10.7.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"ws": "^8.18.0"
"react-dom": "^18.3.1"
},
"devDependencies": {
"@types/react": "^18.3.3",