Files
todo-tests/packages/server/src/index.ts
Julien Valverdé c3560c41bc Cleanup
2024-07-17 05:15:01 +02:00

117 lines
3.5 KiB
TypeScript

import { BunRuntime } from "@effect/platform-bun"
import { Todo } from "@todo-tests/common/data"
import { Duration, Effect, Layer, Logger, Match, Option } from "effect"
import { ServerConfig } from "./ServerConfig"
import { Services } from "./Services"
import { ExpressApp } from "./http/ExpressApp"
import { ExpressHTTPServer } from "./http/ExpressHTTPServer"
import { RPCPlaygroundRoute } from "./rpc/RPCPlaygroundRoute"
import { RPCRoute } from "./rpc/RPCRoute"
import { RPCRouter } from "./rpc/RPCRouter"
import { RPCWebSocketServer } from "./rpc/RPCWebSocketServer"
import { RPCProcedureBuilder } from "./rpc/procedures/RPCProcedureBuilder"
import { TodoRepository } from "./todo/TodoRepository"
import { TRPCBuilder } from "./trpc/TRPCBuilder"
import { TRPCContextCreator } from "./trpc/TRPCContextCreator"
const ServerDev = Layer.empty.pipe(
Layer.provideMerge(RPCRoute.Live),
Layer.provideMerge(RPCPlaygroundRoute.Dev),
Layer.provideMerge(RPCWebSocketServer.Live),
Layer.provideMerge(RPCRouter.Live),
Layer.provideMerge(RPCProcedureBuilder.Live),
Layer.provideMerge(TRPCBuilder.Live),
Layer.provideMerge(TRPCContextCreator.Live),
Layer.provideMerge(ExpressHTTPServer.Live),
Layer.provideMerge(ExpressApp.Live),
)
const ServerLive = Layer.empty.pipe(
Layer.provideMerge(RPCRoute.Live),
Layer.provideMerge(RPCPlaygroundRoute.Live),
Layer.provideMerge(RPCWebSocketServer.Live),
Layer.provideMerge(RPCRouter.Live),
Layer.provideMerge(RPCProcedureBuilder.Live),
Layer.provideMerge(TRPCBuilder.Live),
Layer.provideMerge(TRPCContextCreator.Live),
Layer.provideMerge(ExpressHTTPServer.Live),
Layer.provideMerge(ExpressApp.Live),
)
const main = Effect.gen(function*() {
const mode = yield* ServerConfig.mode
const todos = yield* TodoRepository
// yield* Effect.fork(
// todos.todos.changes.pipe(
// Stream.runForEach(values => Effect.gen(function*() {
// yield* Console.log("Todos updated:")
// yield* Console.log(values)
// }))
// )
// )
yield* todos.add(new Todo({
id: Option.none(),
order: 0,
content: "Sort the socks",
due: Option.none(),
completed: false,
createdAt: new Date(),
updatedAt: new Date(),
}))
yield* todos.add(new Todo({
id: Option.none(),
order: 1,
content: "Sort the dog",
due: Option.none(),
completed: false,
createdAt: new Date(),
updatedAt: new Date(),
}))
yield* todos.add(new Todo({
id: Option.none(),
order: 2,
content: "Sort the car",
due: Option.none(),
completed: false,
createdAt: new Date(),
updatedAt: new Date(),
}))
yield* Effect.fork(
todos.add(new Todo({
id: Option.none(),
order: 1,
content: "Clean the pool",
due: Option.none(),
completed: false,
createdAt: new Date(),
updatedAt: new Date(),
})).pipe(
Effect.delay(Duration.seconds(5))
)
)
return yield* Layer.launch(Match.value(mode).pipe(
Match.when("development", () => ServerDev),
Match.when("production", () => ServerLive),
Match.exhaustive,
))
})
BunRuntime.runMain(main.pipe(
Effect.provide(Services.Dev),
Effect.provide(Logger.structured),
Effect.scoped,
))