Initial version (#1)
All checks were successful
Build / build (push) Successful in 41s
Lint / lint (push) Successful in 13s

Co-authored-by: Julien Valverdé <julien.valverde@mailo.com>
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2025-09-18 01:26:10 +02:00
parent ab2fff9a9f
commit c0cac90a66
47 changed files with 1633 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
import { Config, Schema } from "effect"
export const mode = Schema.Config("NODE_ENV",
Schema.compose(Schema.String, Schema.Literal("development", "production"))
).pipe(
Config.withDefault("development")
)
export const httpPort = Config.withDefault(Config.port("SERVER_HTTP_PORT"), 80)

View File

@@ -0,0 +1 @@
export * as ServerConfig from "./ServerConfig"

View File

@@ -0,0 +1,17 @@
import { BunContext, BunHttpServer, BunRuntime } from "@effect/platform-bun"
import { Effect, Layer } from "effect"
import { ServerConfig } from "./config"
import { Server } from "./server"
Layer.launch(Server).pipe(
Effect.provide(Layer.empty.pipe(
Layer.provideMerge(ServerConfig.httpPort.pipe(
Effect.map(port => BunHttpServer.layer({ port })),
Layer.unwrapEffect,
)),
Layer.provideMerge(BunContext.layer),
)),
BunRuntime.runMain,
)

View File

@@ -0,0 +1,18 @@
import { NodeContext, NodeHttpServer, NodeRuntime } from "@effect/platform-node"
import { Effect, Layer } from "effect"
import { createServer } from "node:http"
import { ServerConfig } from "./config"
import { Server } from "./server"
Layer.launch(Server).pipe(
Effect.provide(Layer.empty.pipe(
Layer.provideMerge(ServerConfig.httpPort.pipe(
Effect.map(port => NodeHttpServer.layer(createServer, { port })),
Layer.unwrapEffect,
)),
Layer.provideMerge(NodeContext.layer),
)),
NodeRuntime.runMain,
)

View File

@@ -0,0 +1,66 @@
import { FileSystem, HttpMiddleware, HttpRouter, HttpServer, HttpServerRequest, HttpServerResponse, Path } from "@effect/platform"
import { RpcServer } from "@effect/rpc"
import { CommonConfig } from "@website/common/config"
import { WebRpc, WebRpcSerializationDevelopment, WebRpcSerializationProduction } from "@website/common/webrpc"
import { Duration, Effect, flow, Layer } from "effect"
import { WebRpcLive } from "./webrpc"
const router = HttpRouter.empty
const makeWebRpcRoute = Effect.all([
CommonConfig.webRpcHttpPath,
RpcServer.toHttpApp(WebRpc),
]).pipe(
Effect.map(([path, app]) => HttpRouter.mountApp(path, app)),
Effect.provide(WebRpcLive),
)
const makeProductionWebappMiddleware = Effect.gen(function*() {
const path = yield* Path.Path
const fs = yield* FileSystem.FileSystem
const dist = path.join(yield* path.fromFileUrl(new URL(".", import.meta.resolve("@website/webapp"))), "dist")
return () => Effect.gen(function*() {
const req = yield* HttpServerRequest.HttpServerRequest
const source = path.join(dist, req.url)
const isValid = yield* fs.stat(source).pipe(
Effect.andThen(stat => stat.type === "File"),
Effect.catchAll(() => Effect.succeed(false)),
)
return yield* HttpServerResponse.setHeader(
yield* HttpServerResponse.file(isValid ? source : path.join(dist, "index.html")),
"Cache-Control",
`public, max-age=${Duration.toSeconds("365 days")}, immutable`
)
})
})
export const HttpAppDevelopment = Effect.provide(makeWebRpcRoute, WebRpcSerializationDevelopment).pipe(
Effect.map(serveWebRpc => router.pipe(
serveWebRpc,
HttpServer.serve(flow(
HttpMiddleware.logger,
HttpMiddleware.xForwardedHeaders,
)),
HttpServer.withLogAddress,
)),
Layer.unwrapScoped,
)
export const HttpAppProduction = Effect.all([
Effect.provide(makeWebRpcRoute, WebRpcSerializationProduction),
makeProductionWebappMiddleware,
]).pipe(
Effect.map(([serveWebRpc, serveProductionWebapp]) => router.pipe(
serveWebRpc,
serveProductionWebapp,
HttpServer.serve(HttpMiddleware.xForwardedHeaders),
HttpServer.withLogAddress,
)),
Layer.unwrapScoped,
)

View File

@@ -0,0 +1,31 @@
import { NodeSdk } from "@effect/opentelemetry"
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"
import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base"
import { Effect, flow, Layer, Logger, Match } from "effect"
import { ServerConfig } from "./config"
import { HttpAppDevelopment, HttpAppProduction } from "./http"
const ServerDevelopment = Layer.empty.pipe(
Layer.provideMerge(NodeSdk.layer(() => ({
resource: { serviceName: "server" },
spanProcessor: new BatchSpanProcessor(new OTLPTraceExporter({ url: "http://tempo:4318/v1/traces" }))
}))),
Layer.provideMerge(HttpAppDevelopment),
)
const ServerProduction = Layer.empty.pipe(
Layer.provideMerge(HttpAppProduction),
)
export const Server = ServerConfig.mode.pipe(
Effect.map(flow(
Match.value,
Match.when("development", () => ServerDevelopment),
Match.when("production", () => ServerProduction),
Match.exhaustive,
)),
Layer.unwrapEffect,
Layer.provideMerge(Logger.pretty),
)

View File

@@ -0,0 +1,9 @@
import { WebRpcTest } from "@website/common/webrpc"
import { Effect, Layer } from "effect"
export const PingV1Live = WebRpcTest.WebRpcTest.toLayerHandler("Test.PingV1", Effect.succeed(() => Effect.succeed("pong" as const)))
export const WebRpcTestLive = Layer.mergeAll(
PingV1Live,
) satisfies ReturnType<typeof WebRpcTest.WebRpcTest.toLayer>

View File

@@ -0,0 +1,10 @@
import type { WebRpc } from "@website/common/webrpc"
import { Layer } from "effect"
import * as WebRpcTestLive from "./WebRpcTestLive"
export const WebRpcLive = Layer.mergeAll(
WebRpcTestLive.WebRpcTestLive,
) satisfies ReturnType<typeof WebRpc.toLayer>
export * as WebRpcTestLive from "./WebRpcTestLive"