From 5f15dedfbf91a44a6f7ed4a7d67d1f32ac4d9b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Thu, 4 Jul 2024 21:26:16 +0200 Subject: [PATCH] Refactoring --- packages/server/src/{App.ts => Services.ts} | 4 +-- packages/server/src/express/ExpressApp.ts | 4 ++- .../server/src/express/ExpressHTTPServer.ts | 30 +++++++++---------- packages/server/src/index.ts | 4 +-- packages/server/src/rpc/RPCServer.ts | 6 ++-- packages/server/src/trpc/TRPCBuilder.ts | 5 +++- packages/server/src/trpc/TRPCEffectRuntime.ts | 16 +++++----- 7 files changed, 36 insertions(+), 33 deletions(-) rename packages/server/src/{App.ts => Services.ts} (51%) diff --git a/packages/server/src/App.ts b/packages/server/src/Services.ts similarity index 51% rename from packages/server/src/App.ts rename to packages/server/src/Services.ts index 38df96c..be92bd1 100644 --- a/packages/server/src/App.ts +++ b/packages/server/src/Services.ts @@ -2,8 +2,8 @@ import { Layer } from "effect" import { TodoRepositoryLive } from "./TodoRepository" -export const AppLive = Layer.mergeAll( +export const ServicesLive = Layer.mergeAll( TodoRepositoryLive ) -export type AppR = Layer.Layer.Success +export type Services = Layer.Layer.Success diff --git a/packages/server/src/express/ExpressApp.ts b/packages/server/src/express/ExpressApp.ts index ed7ea08..7750610 100644 --- a/packages/server/src/express/ExpressApp.ts +++ b/packages/server/src/express/ExpressApp.ts @@ -4,4 +4,6 @@ import express from "express" export class ExpressApp extends Context.Tag("Express")>() {} -export const ExpressAppLive = Layer.sync(ExpressApp, () => express()) +export module ExpressApp { + export const Live = Layer.sync(ExpressApp, () => express()) +} diff --git a/packages/server/src/express/ExpressHTTPServer.ts b/packages/server/src/express/ExpressHTTPServer.ts index f75d603..9080588 100644 --- a/packages/server/src/express/ExpressHTTPServer.ts +++ b/packages/server/src/express/ExpressHTTPServer.ts @@ -1,20 +1,20 @@ import { Config, Effect, Layer } from "effect" -import { ExpressApp, ExpressAppLive } from "./ExpressApp" +import { ExpressApp } from "./ExpressApp" -export const ExpressHTTPServerLive = Layer.scopedDiscard(Effect.gen(function*() { - const app = yield* ExpressApp - const port = yield* Config.number("PORT").pipe(Config.withDefault(8080)) +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)) - yield* Effect.acquireRelease( - Effect.sync(() => - app.listen(port, () => console.log(`HTTP server listening on ${ port }.`)) - ), + yield* Effect.acquireRelease( + Effect.sync(() => + app.listen(port, () => console.log(`HTTP server listening on ${ port }.`)) + ), - server => Effect.sync(() => - server.close() - ), - ) -})).pipe( - Layer.provide(ExpressAppLive) -) + server => Effect.sync(() => + server.close() + ), + ) + })) +} diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index 6a51e66..92f72f3 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -2,7 +2,7 @@ import { BunRuntime } from "@effect/platform-bun" import { Todo } from "@todo-tests/common/data" import { Identifiable } from "@todo-tests/common/traits" import { Array, Duration, Effect, Fiber, Layer, Option, Stream } from "effect" -import { AppLive } from "./App" +import { ServicesLive } from "./Services" import { TodoRepository, createDefaultTodos } from "./TodoRepository" import { TRPCEffectRuntimeLive } from "./trpc/TRPCEffectRuntime" @@ -58,7 +58,7 @@ const main = Effect.gen(function*() { const runnableMain = main.pipe( Effect.provide( Layer.mergeAll( - AppLive, + ServicesLive, TRPCEffectRuntimeLive, ) ) diff --git a/packages/server/src/rpc/RPCServer.ts b/packages/server/src/rpc/RPCServer.ts index d69a0b8..1cda9d8 100644 --- a/packages/server/src/rpc/RPCServer.ts +++ b/packages/server/src/rpc/RPCServer.ts @@ -1,6 +1,6 @@ import { createExpressMiddleware } from "@trpc/server/adapters/express" import { Config, Effect, Layer } from "effect" -import { ExpressApp, ExpressAppLive } from "../express/ExpressApp" +import { ExpressApp } from "../express/ExpressApp" import { createTRPCContext } from "../trpc/context" import { router } from "./routers" @@ -16,6 +16,4 @@ export const RPCServerLive = Layer.effectDiscard(Effect.gen(function*() { createContext: createTRPCContext, }), ) -})).pipe( - Layer.provide(ExpressAppLive) -) +})) diff --git a/packages/server/src/trpc/TRPCBuilder.ts b/packages/server/src/trpc/TRPCBuilder.ts index 2fddbe5..c405117 100644 --- a/packages/server/src/trpc/TRPCBuilder.ts +++ b/packages/server/src/trpc/TRPCBuilder.ts @@ -6,4 +6,7 @@ import type { TRPCContext } from "./context" const createTRPC = () => initTRPC.context().create() export class TRPCBuilder extends Context.Tag("TRPCBuilder")>() {} -export const TRPCBuilderLive = Layer.sync(TRPCBuilder, createTRPC) + +export module TRPCBuilder { + export const Live = Layer.sync(TRPCBuilder, createTRPC) +} diff --git a/packages/server/src/trpc/TRPCEffectRuntime.ts b/packages/server/src/trpc/TRPCEffectRuntime.ts index 207843a..8b83773 100644 --- a/packages/server/src/trpc/TRPCEffectRuntime.ts +++ b/packages/server/src/trpc/TRPCEffectRuntime.ts @@ -1,19 +1,19 @@ import { Context, Effect, Layer, Runtime } from "effect" import type { RuntimeFiber } from "effect/Fiber" -import { AppLive, type AppR } from "../App" +import type { Services } from "../Services" export class TRPCEffectRuntime extends Context.Tag("TRPCEffectRuntime")( - self: Effect.Effect, + self: Effect.Effect, options?: Runtime.RunForkOptions, ) => RuntimeFiber >() {} -export const TRPCEffectRuntimeLive = Layer.effect(TRPCEffectRuntime, - Effect.runtime().pipe( - Effect.map(Runtime.runFork) +export module TRPCEffectRuntime { + export const Live = Layer.effect(TRPCEffectRuntime, + Effect.runtime().pipe( + Effect.map(Runtime.runFork) + ) ) -).pipe( - Layer.provide(AppLive) -) +}