Config refactoring
This commit is contained in:
20
packages/server/src/ServerConfig.ts
Normal file
20
packages/server/src/ServerConfig.ts
Normal file
@@ -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"))
|
||||
}
|
||||
@@ -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"))
|
||||
@@ -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 }`)))
|
||||
}),
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user