|
| 1 | +import { OpenAPIHandler } from "@orpc/openapi/fetch"; |
| 2 | +import { OpenAPIReferencePlugin } from "@orpc/openapi/plugins"; |
| 3 | +import { onError, os } from "@orpc/server"; |
| 4 | +import { CORSPlugin } from "@orpc/server/plugins"; |
| 5 | +import { ZodToJsonSchemaConverter } from "@orpc/zod/zod4"; |
| 6 | +import { serve } from "bun"; |
| 7 | +import { type Effect as EffectType, Effect, pipe } from "effect"; |
| 8 | +import { makeEffectORPC } from "effect-orpc"; |
| 9 | +import { withFiberContext } from "effect-orpc/node"; |
| 10 | +import { Hono } from "hono"; |
| 11 | +import { requestId } from "hono/request-id"; |
| 12 | + |
| 13 | +import { runtime } from "./runtime"; |
| 14 | +import { OrderService } from "./services/order"; |
| 15 | + |
| 16 | +const port = Number(process.env.PORT ?? "3000"); |
| 17 | + |
| 18 | +const app = new Hono(); |
| 19 | +app.use("*", requestId()); |
| 20 | + |
| 21 | +app.use("/*", async (c, next) => { |
| 22 | + const { method, path } = c.req; |
| 23 | + const currentRequestId = c.get("requestId"); |
| 24 | + |
| 25 | + const requestEffect = Effect.gen(function* () { |
| 26 | + yield* Effect.annotateLogsScoped({ |
| 27 | + requestId: currentRequestId, |
| 28 | + service: "backend-service", |
| 29 | + }); |
| 30 | + yield* Effect.logInfo(`[Request] ${method} ${path}`); |
| 31 | + yield* withFiberContext(() => next()); |
| 32 | + yield* Effect.logInfo(`[Response] ${method} ${path} (${c.res.status})`); |
| 33 | + }).pipe(Effect.scoped, Effect.withSpan(`${method} ${path}`)); |
| 34 | + |
| 35 | + await runtime.runPromise( |
| 36 | + requestEffect as EffectType.Effect<void, unknown, never>, |
| 37 | + ); |
| 38 | +}); |
| 39 | + |
| 40 | +const o = makeEffectORPC(runtime, os); |
| 41 | + |
| 42 | +const router = { |
| 43 | + orders: o.route({ path: "/orders", method: "GET" }).effect(function* () { |
| 44 | + yield* Effect.logInfo("Handler: GET /orders - listing all orders"); |
| 45 | + return yield* OrderService.listOrders(); |
| 46 | + }), |
| 47 | + test: o.route({ path: "/test", method: "GET" }).effect(function* () { |
| 48 | + return "ok"; |
| 49 | + }), |
| 50 | +}; |
| 51 | + |
| 52 | +const openAPIHandler = new OpenAPIHandler(router, { |
| 53 | + plugins: [ |
| 54 | + new CORSPlugin(), |
| 55 | + new OpenAPIReferencePlugin({ |
| 56 | + schemaConverters: [new ZodToJsonSchemaConverter()], |
| 57 | + }), |
| 58 | + ], |
| 59 | + interceptors: [ |
| 60 | + onError(async (error) => { |
| 61 | + await runtime.runPromise(pipe(Effect.logError("oRPC Error", error))); |
| 62 | + }), |
| 63 | + ], |
| 64 | +}); |
| 65 | + |
| 66 | +app.use("/*", async (c, next) => { |
| 67 | + const { matched, response } = await openAPIHandler.handle(c.req.raw, { |
| 68 | + prefix: "/api", |
| 69 | + }); |
| 70 | + |
| 71 | + if (matched) { |
| 72 | + return c.newResponse(response.body, response); |
| 73 | + } |
| 74 | + |
| 75 | + await next(); |
| 76 | +}); |
| 77 | + |
| 78 | +const server = serve({ |
| 79 | + fetch: app.fetch, |
| 80 | + port, |
| 81 | +}); |
| 82 | + |
| 83 | +await runtime.runPromise( |
| 84 | + pipe( |
| 85 | + Effect.logInfo(`Server started on http://localhost:${port}`), |
| 86 | + Effect.annotateLogs("service", "backend-service"), |
| 87 | + ), |
| 88 | +); |
| 89 | + |
| 90 | +process.on("SIGINT", () => { |
| 91 | + server.stop(); |
| 92 | + process.exit(0); |
| 93 | +}); |
0 commit comments