diff --git a/packages/server/src/ServerConfig.ts b/packages/server/src/ServerConfig.ts new file mode 100644 index 0000000..f1a14a7 --- /dev/null +++ b/packages/server/src/ServerConfig.ts @@ -0,0 +1,20 @@ +import { Schema as S } from "@effect/schema" +import { Config } from "effect" + + +export module ServerConfig { + export const mode = Config.string("NODE_ENV").pipe( + Config.validate({ + message: "Expected 'development' or 'production'", + validation: S.is(S.Union( + S.Literal("development"), + S.Literal("production"), + )), + }), + Config.withDefault("development"), + ) + + export const httpPort = Config.number("HTTP_PORT").pipe(Config.withDefault(8080)) + export const rpcHTTPRoot = Config.string("RPC_HTTP_ROOT").pipe(Config.withDefault("/rpc")) + export const rpcHTTPPlaygroundRoot = Config.string("RPC_HTTP_PLAYGROUND_ROOT").pipe(Config.withDefault("/rpc/playground")) +} diff --git a/packages/server/src/config.ts b/packages/server/src/config.ts deleted file mode 100644 index da44c81..0000000 --- a/packages/server/src/config.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Schema as S } from "@effect/schema" -import { Config } from "effect" - - -export const mode = Config.string("NODE_ENV").pipe( - Config.withDefault("development"), - Config.validate({ - message: "Expected 'development' or 'production'", - validation: S.is(S.Union( - S.Literal("development"), - S.Literal("production"), - )), - }) -) - -export const httpPort = Config.number("HTTP_PORT").pipe(Config.withDefault(8080)) -export const rpcHTTPRoot = Config.string("RPC_HTTP_ROOT").pipe(Config.withDefault("/rpc")) -export const rpcHTTPPlaygroundRoot = Config.string("RPC_HTTP_PLAYGROUND_ROOT").pipe(Config.withDefault("/rpc/playground")) diff --git a/packages/server/src/http/ExpressHTTPServer.ts b/packages/server/src/http/ExpressHTTPServer.ts index dbde7ac..c0b2453 100644 --- a/packages/server/src/http/ExpressHTTPServer.ts +++ b/packages/server/src/http/ExpressHTTPServer.ts @@ -1,6 +1,6 @@ import { Context, Effect, Layer, Runtime } from "effect" import { Server } from "node:http" -import { httpPort } from "../config" +import { ServerConfig } from "../ServerConfig" import { ExpressApp } from "./ExpressApp" @@ -14,7 +14,7 @@ export module ExpressHTTPServer { ) const app = yield* ExpressApp - const port = yield* httpPort + const port = yield* ServerConfig.httpPort return app.listen(port, () => runSync(Effect.logInfo(`HTTP server listening on ${ port }`))) }), diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index 230993d..78b0150 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -1,6 +1,7 @@ import { BunRuntime } from "@effect/platform-bun" import { Todo } from "@todo-tests/common/data" -import { Duration, Effect, Layer, Option } from "effect" +import { Duration, Effect, Layer, Match, Option } from "effect" +import { ServerConfig } from "./ServerConfig" import { Services } from "./Services" import { ExpressApp } from "./http/ExpressApp" import { ExpressHTTPServer } from "./http/ExpressHTTPServer" @@ -30,8 +31,24 @@ const ServerDev = Layer.empty.pipe( Layer.provideMerge(ExpressApp.Live), ) +const ServerLive = Layer.empty.pipe( + Layer.provideMerge(RPCRoute.Live), + Layer.provideMerge(RPCPlaygroundRoute.Live), + Layer.provideMerge(RPCWebSocketHandler.Live), + + Layer.provideMerge(RPCRouter.Live), + Layer.provideMerge(RPCProcedureBuilder.Live), + Layer.provideMerge(TRPCBuilder.Live), + Layer.provideMerge(TRPCContextCreator.Live), + + Layer.provideMerge(WebSocketServer.Live), + Layer.provideMerge(ExpressHTTPServer.Live), + Layer.provideMerge(ExpressApp.Live), +) + const main = Effect.gen(function*() { + const mode = yield* ServerConfig.mode const todos = yield* TodoRepository yield* todos.add(new Todo({ @@ -79,7 +96,11 @@ const main = Effect.gen(function*() { ) - yield* Layer.launch(ServerDev) + return yield* Layer.launch(Match.value(mode).pipe( + Match.when("development", () => ServerDev), + Match.when("production", () => ServerLive), + Match.exhaustive, + )) }) BunRuntime.runMain(main.pipe( diff --git a/packages/server/src/rpc/RPCPlaygroundRoute.ts b/packages/server/src/rpc/RPCPlaygroundRoute.ts index 403c939..8ed0293 100644 --- a/packages/server/src/rpc/RPCPlaygroundRoute.ts +++ b/packages/server/src/rpc/RPCPlaygroundRoute.ts @@ -1,6 +1,6 @@ import { Effect, Layer } from "effect" import { expressHandler } from "trpc-playground/handlers/express" -import { rpcHTTPPlaygroundRoot, rpcHTTPRoot } from "../config" +import { ServerConfig } from "../ServerConfig" import { ExpressApp } from "../http/ExpressApp" import { RPCRouter } from "./RPCRouter" @@ -10,10 +10,10 @@ export module RPCPlaygroundRoute { export const Dev = Layer.effectDiscard(Effect.gen(function*() { const app = yield* ExpressApp - const playgroundEndpoint = yield* rpcHTTPPlaygroundRoot + const playgroundEndpoint = yield* ServerConfig.rpcHTTPPlaygroundRoot const handler = expressHandler({ - trpcApiEndpoint: yield* rpcHTTPRoot, + trpcApiEndpoint: yield* ServerConfig.rpcHTTPRoot, playgroundEndpoint, router: yield* RPCRouter, }) diff --git a/packages/server/src/rpc/RPCRoute.ts b/packages/server/src/rpc/RPCRoute.ts index e5e7593..d4d25bb 100644 --- a/packages/server/src/rpc/RPCRoute.ts +++ b/packages/server/src/rpc/RPCRoute.ts @@ -1,6 +1,6 @@ import { createExpressMiddleware } from "@trpc/server/adapters/express" import { Effect, Layer } from "effect" -import { rpcHTTPRoot } from "../config" +import { ServerConfig } from "../ServerConfig" import { ExpressApp } from "../http/ExpressApp" import { TRPCContextCreator } from "../trpc/TRPCContextCreator" import { RPCRouter } from "./RPCRouter" @@ -10,7 +10,7 @@ export module RPCRoute { export const Live = Layer.effectDiscard(Effect.gen(function*() { const app = yield* ExpressApp - app.use(yield* rpcHTTPRoot, + app.use(yield* ServerConfig.rpcHTTPRoot, createExpressMiddleware({ router: yield* RPCRouter, createContext: (yield* TRPCContextCreator).createExpressContext,