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" 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 module ExpressApp {
export const Live = Layer.sync(ExpressApp, () => express()) 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 { httpPort } from "../config"
import { ExpressApp } from "./ExpressApp" import { ExpressApp } from "./ExpressApp"
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.scopedDiscard(Effect.acquireRelease(
Effect.gen(function*() { Effect.gen(function*() {
const runSync = yield* Effect.runtime().pipe(
Effect.map(Runtime.runSync)
)
const app = yield* ExpressApp const app = yield* ExpressApp
const port = yield* httpPort const port = yield* httpPort
return app.listen(port, () => console.log(`HTTP server listening on ${ port }.`)) return app.listen(port, () => runSync(Effect.logInfo(`HTTP server listening on ${ port }`)))
}), }),
server => Effect.sync(() => server => Effect.gen(function*() {
server.close() 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 { applyWSSHandler } from "@trpc/server/adapters/ws"
import { Effect, Layer } from "effect" import { Effect, Layer, Runtime } from "effect"
import ws from "ws" import { WebSocketServer } from "ws"
import { websocketPort } from "../config" import { websocketPort } from "../config"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator" import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
import { RPCRouter } from "./RPCRouter" import { RPCRouter } from "./RPCRouter"
@@ -9,7 +9,11 @@ import { RPCRouter } from "./RPCRouter"
export module RPCWebSocketServer { export module RPCWebSocketServer {
export const Live = Layer.scopedDiscard(Effect.acquireRelease( export const Live = Layer.scopedDiscard(Effect.acquireRelease(
Effect.gen(function*() { 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({ const handler = applyWSSHandler({
wss, wss,

View File

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