RPC work
This commit is contained in:
@@ -2,8 +2,8 @@ import { Layer } from "effect"
|
||||
import { TodoRepositoryLive } from "./TodoRepository"
|
||||
|
||||
|
||||
export const MainLive = Layer.mergeAll(
|
||||
export const AppLive = Layer.mergeAll(
|
||||
TodoRepositoryLive
|
||||
)
|
||||
|
||||
export type MainR = Layer.Layer.Success<typeof MainLive>
|
||||
export type AppR = Layer.Layer.Success<typeof AppLive>
|
||||
@@ -1,35 +0,0 @@
|
||||
import { initTRPC } from "@trpc/server"
|
||||
import { Context, Effect, Layer, Runtime } from "effect"
|
||||
import type { RuntimeFiber } from "effect/Fiber"
|
||||
import { MainLive, type MainR } from "./Main"
|
||||
import { TodoRepository } from "./TodoRepository"
|
||||
|
||||
|
||||
export class TRPCEffectRuntime extends Context.Tag("TRPCEffectRuntime")<TRPCEffectRuntime,
|
||||
<A, E>(
|
||||
self: Effect.Effect<A, E, MainR>,
|
||||
options?: Runtime.RunForkOptions,
|
||||
) => RuntimeFiber<A, E>
|
||||
>() {}
|
||||
|
||||
export const TRPCEffectRuntimeLive = Layer.effect(TRPCEffectRuntime,
|
||||
Effect.runtime<MainR>().pipe(
|
||||
Effect.map(Runtime.runFork)
|
||||
)
|
||||
).pipe(
|
||||
Layer.provide(MainLive)
|
||||
)
|
||||
|
||||
|
||||
export const t = initTRPC.create()
|
||||
|
||||
const testRouter = Effect.gen(function*() {
|
||||
const run = yield* TRPCEffectRuntime
|
||||
|
||||
return t.router({
|
||||
test1: t.procedure.query(() => run(Effect.gen(function*() {
|
||||
const todos = yield* TodoRepository
|
||||
return "test"
|
||||
}))),
|
||||
})
|
||||
})
|
||||
@@ -1,7 +0,0 @@
|
||||
import { Context, Layer } from "effect"
|
||||
import express from "express"
|
||||
|
||||
|
||||
export class Express extends Context.Tag("Express")<Express, ReturnType<typeof express>>() {}
|
||||
|
||||
export const ExpressLive = Layer.sync(Express, () => express())
|
||||
7
packages/server/src/express/ExpressApp.ts
Normal file
7
packages/server/src/express/ExpressApp.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Context, Layer } from "effect"
|
||||
import express from "express"
|
||||
|
||||
|
||||
export class ExpressApp extends Context.Tag("Express")<ExpressApp, ReturnType<typeof express>>() {}
|
||||
|
||||
export const ExpressAppLive = Layer.sync(ExpressApp, () => express())
|
||||
@@ -1,9 +1,9 @@
|
||||
import { Config, Effect, Layer } from "effect"
|
||||
import { Express, ExpressLive } from "./Express"
|
||||
import { ExpressApp, ExpressAppLive } from "./ExpressApp"
|
||||
|
||||
|
||||
export const HTTPServerLive = Layer.scopedDiscard(Effect.gen(function*() {
|
||||
const app = yield* Express
|
||||
export const ExpressHTTPServerLive = Layer.scopedDiscard(Effect.gen(function*() {
|
||||
const app = yield* ExpressApp
|
||||
const port = yield* Config.number("PORT").pipe(Config.withDefault(8080))
|
||||
|
||||
yield* Effect.acquireRelease(
|
||||
@@ -16,5 +16,5 @@ export const HTTPServerLive = Layer.scopedDiscard(Effect.gen(function*() {
|
||||
),
|
||||
)
|
||||
})).pipe(
|
||||
Layer.provide(ExpressLive)
|
||||
Layer.provide(ExpressAppLive)
|
||||
)
|
||||
@@ -2,9 +2,9 @@ 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 { MainLive } from "./Main"
|
||||
import { TRPCEffectRuntimeLive } from "./TRPCEffectRuntime"
|
||||
import { AppLive } from "./App"
|
||||
import { TodoRepository, createDefaultTodos } from "./TodoRepository"
|
||||
import { TRPCEffectRuntimeLive } from "./trpc/TRPCEffectRuntime"
|
||||
|
||||
|
||||
const watchTodoChanges = Effect.gen(function*() {
|
||||
@@ -58,8 +58,8 @@ const main = Effect.gen(function*() {
|
||||
const runnableMain = main.pipe(
|
||||
Effect.provide(
|
||||
Layer.mergeAll(
|
||||
AppLive,
|
||||
TRPCEffectRuntimeLive,
|
||||
MainLive,
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
13
packages/server/src/rpc/index.ts
Normal file
13
packages/server/src/rpc/index.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Effect, Layer } from "effect"
|
||||
import { TRPCBuilder, TRPCBuilderLive } from "../trpc/TRPCBuilder"
|
||||
|
||||
|
||||
export const RPCRouter = Layer.effectDiscard(Effect.gen(function*() {
|
||||
const t = yield* TRPCBuilder
|
||||
|
||||
t.router({
|
||||
|
||||
})
|
||||
})).pipe(
|
||||
Layer.provide(TRPCBuilderLive)
|
||||
)
|
||||
7
packages/server/src/trpc/TRPCBuilder.ts
Normal file
7
packages/server/src/trpc/TRPCBuilder.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { initTRPC } from "@trpc/server"
|
||||
import { Context, Layer } from "effect"
|
||||
|
||||
|
||||
export class TRPCBuilder extends Context.Tag("TRPCBuilder")<TRPCBuilder, ReturnType<typeof initTRPC.create>>() {}
|
||||
|
||||
export const TRPCBuilderLive = Layer.sync(TRPCBuilder, () => initTRPC.create())
|
||||
19
packages/server/src/trpc/TRPCEffectRuntime.ts
Normal file
19
packages/server/src/trpc/TRPCEffectRuntime.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Context, Effect, Layer, Runtime } from "effect"
|
||||
import type { RuntimeFiber } from "effect/Fiber"
|
||||
import { AppLive, type AppR } from "../App"
|
||||
|
||||
|
||||
export class TRPCEffectRuntime extends Context.Tag("TRPCEffectRuntime")<TRPCEffectRuntime,
|
||||
<A, E>(
|
||||
self: Effect.Effect<A, E, AppR>,
|
||||
options?: Runtime.RunForkOptions,
|
||||
) => RuntimeFiber<A, E>
|
||||
>() {}
|
||||
|
||||
export const TRPCEffectRuntimeLive = Layer.effect(TRPCEffectRuntime,
|
||||
Effect.runtime<AppR>().pipe(
|
||||
Effect.map(Runtime.runFork)
|
||||
)
|
||||
).pipe(
|
||||
Layer.provide(AppLive)
|
||||
)
|
||||
Reference in New Issue
Block a user