Refactoring
This commit is contained in:
@@ -2,8 +2,8 @@ import { Layer } from "effect"
|
|||||||
import { TodoRepositoryLive } from "./TodoRepository"
|
import { TodoRepositoryLive } from "./TodoRepository"
|
||||||
|
|
||||||
|
|
||||||
export const AppLive = Layer.mergeAll(
|
export const ServicesLive = Layer.mergeAll(
|
||||||
TodoRepositoryLive
|
TodoRepositoryLive
|
||||||
)
|
)
|
||||||
|
|
||||||
export type AppR = Layer.Layer.Success<typeof AppLive>
|
export type Services = Layer.Layer.Success<typeof ServicesLive>
|
||||||
@@ -4,4 +4,6 @@ import express from "express"
|
|||||||
|
|
||||||
export class ExpressApp extends Context.Tag("Express")<ExpressApp, ReturnType<typeof express>>() {}
|
export class ExpressApp extends Context.Tag("Express")<ExpressApp, ReturnType<typeof express>>() {}
|
||||||
|
|
||||||
export const ExpressAppLive = Layer.sync(ExpressApp, () => express())
|
export module ExpressApp {
|
||||||
|
export const Live = Layer.sync(ExpressApp, () => express())
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,20 +1,20 @@
|
|||||||
import { Config, Effect, Layer } from "effect"
|
import { Config, Effect, Layer } from "effect"
|
||||||
import { ExpressApp, ExpressAppLive } from "./ExpressApp"
|
import { ExpressApp } from "./ExpressApp"
|
||||||
|
|
||||||
|
|
||||||
export const ExpressHTTPServerLive = Layer.scopedDiscard(Effect.gen(function*() {
|
export module ExpressHTTPServer {
|
||||||
const app = yield* ExpressApp
|
export const Live = Layer.scopedDiscard(Effect.gen(function*() {
|
||||||
const port = yield* Config.number("PORT").pipe(Config.withDefault(8080))
|
const app = yield* ExpressApp
|
||||||
|
const port = yield* Config.number("PORT").pipe(Config.withDefault(8080))
|
||||||
|
|
||||||
yield* Effect.acquireRelease(
|
yield* Effect.acquireRelease(
|
||||||
Effect.sync(() =>
|
Effect.sync(() =>
|
||||||
app.listen(port, () => console.log(`HTTP server listening on ${ port }.`))
|
app.listen(port, () => console.log(`HTTP server listening on ${ port }.`))
|
||||||
),
|
),
|
||||||
|
|
||||||
server => Effect.sync(() =>
|
server => Effect.sync(() =>
|
||||||
server.close()
|
server.close()
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
})).pipe(
|
}))
|
||||||
Layer.provide(ExpressAppLive)
|
}
|
||||||
)
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { BunRuntime } from "@effect/platform-bun"
|
|||||||
import { Todo } from "@todo-tests/common/data"
|
import { Todo } from "@todo-tests/common/data"
|
||||||
import { Identifiable } from "@todo-tests/common/traits"
|
import { Identifiable } from "@todo-tests/common/traits"
|
||||||
import { Array, Duration, Effect, Fiber, Layer, Option, Stream } from "effect"
|
import { Array, Duration, Effect, Fiber, Layer, Option, Stream } from "effect"
|
||||||
import { AppLive } from "./App"
|
import { ServicesLive } from "./Services"
|
||||||
import { TodoRepository, createDefaultTodos } from "./TodoRepository"
|
import { TodoRepository, createDefaultTodos } from "./TodoRepository"
|
||||||
import { TRPCEffectRuntimeLive } from "./trpc/TRPCEffectRuntime"
|
import { TRPCEffectRuntimeLive } from "./trpc/TRPCEffectRuntime"
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ const main = Effect.gen(function*() {
|
|||||||
const runnableMain = main.pipe(
|
const runnableMain = main.pipe(
|
||||||
Effect.provide(
|
Effect.provide(
|
||||||
Layer.mergeAll(
|
Layer.mergeAll(
|
||||||
AppLive,
|
ServicesLive,
|
||||||
TRPCEffectRuntimeLive,
|
TRPCEffectRuntimeLive,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { createExpressMiddleware } from "@trpc/server/adapters/express"
|
import { createExpressMiddleware } from "@trpc/server/adapters/express"
|
||||||
import { Config, Effect, Layer } from "effect"
|
import { Config, Effect, Layer } from "effect"
|
||||||
import { ExpressApp, ExpressAppLive } from "../express/ExpressApp"
|
import { ExpressApp } from "../express/ExpressApp"
|
||||||
import { createTRPCContext } from "../trpc/context"
|
import { createTRPCContext } from "../trpc/context"
|
||||||
import { router } from "./routers"
|
import { router } from "./routers"
|
||||||
|
|
||||||
@@ -16,6 +16,4 @@ export const RPCServerLive = Layer.effectDiscard(Effect.gen(function*() {
|
|||||||
createContext: createTRPCContext,
|
createContext: createTRPCContext,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
})).pipe(
|
}))
|
||||||
Layer.provide(ExpressAppLive)
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -6,4 +6,7 @@ import type { TRPCContext } from "./context"
|
|||||||
const createTRPC = () => initTRPC.context<TRPCContext>().create()
|
const createTRPC = () => initTRPC.context<TRPCContext>().create()
|
||||||
|
|
||||||
export class TRPCBuilder extends Context.Tag("TRPCBuilder")<TRPCBuilder, ReturnType<typeof createTRPC>>() {}
|
export class TRPCBuilder extends Context.Tag("TRPCBuilder")<TRPCBuilder, ReturnType<typeof createTRPC>>() {}
|
||||||
export const TRPCBuilderLive = Layer.sync(TRPCBuilder, createTRPC)
|
|
||||||
|
export module TRPCBuilder {
|
||||||
|
export const Live = Layer.sync(TRPCBuilder, createTRPC)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
import { Context, Effect, Layer, Runtime } from "effect"
|
import { Context, Effect, Layer, Runtime } from "effect"
|
||||||
import type { RuntimeFiber } from "effect/Fiber"
|
import type { RuntimeFiber } from "effect/Fiber"
|
||||||
import { AppLive, type AppR } from "../App"
|
import type { Services } from "../Services"
|
||||||
|
|
||||||
|
|
||||||
export class TRPCEffectRuntime extends Context.Tag("TRPCEffectRuntime")<TRPCEffectRuntime,
|
export class TRPCEffectRuntime extends Context.Tag("TRPCEffectRuntime")<TRPCEffectRuntime,
|
||||||
<A, E>(
|
<A, E>(
|
||||||
self: Effect.Effect<A, E, AppR>,
|
self: Effect.Effect<A, E, Services>,
|
||||||
options?: Runtime.RunForkOptions,
|
options?: Runtime.RunForkOptions,
|
||||||
) => RuntimeFiber<A, E>
|
) => RuntimeFiber<A, E>
|
||||||
>() {}
|
>() {}
|
||||||
|
|
||||||
export const TRPCEffectRuntimeLive = Layer.effect(TRPCEffectRuntime,
|
export module TRPCEffectRuntime {
|
||||||
Effect.runtime<AppR>().pipe(
|
export const Live = Layer.effect(TRPCEffectRuntime,
|
||||||
Effect.map(Runtime.runFork)
|
Effect.runtime<Services>().pipe(
|
||||||
|
Effect.map(Runtime.runFork)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
).pipe(
|
}
|
||||||
Layer.provide(AppLive)
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user