feat: add @effect/rpc

This commit is contained in:
Max Koon
2025-11-28 15:04:42 -05:00
parent 02dd064d99
commit 3ebb7ee796
9 changed files with 154 additions and 32 deletions

View File

@@ -9,6 +9,7 @@
"dependencies": {
"@effect/platform": "^0.93.2",
"@effect/platform-node": "^0.101.1",
"@effect/rpc": "^0.72.2",
"@money/shared": "workspace:*",
"better-auth": "^1.3.27",
"effect": "^3.19.4",

View File

@@ -5,11 +5,12 @@ import * as HttpServerResponse from "@effect/platform/HttpServerResponse";
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
import { createServer } from "http";
import { CorsMiddleware } from "./middleware/cors";
import { AuthRoute } from "./auth/handler";
import { BetterAuthLive } from "./auth/better-auth";
import { WebhookReceiverRoute } from "./webhook";
import { ZeroMutateRoute, ZeroQueryRoute } from "./zero/handler";
import { RpcRoute } from "./rpc/handler";
import { BASE_URL } from "@money/shared";
const RootRoute = HttpLayerRouter.add(
"GET",
@@ -24,13 +25,22 @@ const AllRoutes = Layer.mergeAll(
AuthRoute,
ZeroQueryRoute,
ZeroMutateRoute,
RpcRoute,
WebhookReceiverRoute,
).pipe(
Layer.provide(
HttpLayerRouter.cors({
allowedOrigins: ["https://money.koon.us", `${BASE_URL}:8081`],
allowedMethods: ["POST", "GET", "OPTIONS"],
// allowedHeaders: ["Content-Type", "Authorization", ""],
credentials: true,
}),
),
);
HttpLayerRouter.serve(AllRoutes).pipe(
Layer.provide(NodeHttpServer.layer(createServer, { port: 3000 })),
Layer.provide(BetterAuthLive),
Layer.provide(CorsMiddleware.layer),
Layer.launch,
NodeRuntime.runMain,
);

View File

@@ -8,4 +8,5 @@ export const CorsMiddleware = HttpLayerRouter.middleware(
allowedHeaders: ["Content-Type", "Authorization"],
credentials: true,
}),
{ global: true },
);

View File

@@ -0,0 +1,13 @@
import { RpcSerialization, RpcServer } from "@effect/rpc";
import { Effect, Layer, Schema } from "effect";
import { LinkRpcs, Link } from "@money/shared/rpc";
const LinkHandlers = LinkRpcs.toLayer({
CreateLink: () => Effect.succeed(new Link({ href: "hi" })),
});
export const RpcRoute = RpcServer.layerHttpRouter({
group: LinkRpcs,
path: "/rpc",
protocol: "http",
}).pipe(Layer.provide(LinkHandlers), Layer.provide(RpcSerialization.layerJson));