The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 07:15 UTC
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import { dieOnStore } from "./errors.ts"; |
| 8 | import LinkRoom from "./LinkRoom.ts"; |
| 9 | import { Links, LinksSql } from "./Links.ts"; |
| 10 | import { serve } from "./serve.ts"; |
| 11 | import { ShortyApi } from "./ShortyApi.ts"; |
| 12 | import { NeonStorage } from "./Storage.ts"; |
| 13 | |
| 14 | // #region show |
| 15 | export default Cloudflare.Worker( |
| 16 | "Api", |
| 17 | { main: import.meta.url }, |
| 18 | Effect.gen(function* () { |
| 19 | // #region top |
| 20 | const links = yield* Links; |
| 21 | // #endregion top |
| 22 | // #region rooms |
| 23 | const rooms = yield* LinkRoom; |
| 24 | // #endregion rooms |
| 25 | // #region handlers |
| 26 | |
| 27 | const handlers = HttpApiBuilder.group(ShortyApi, "links", (h) => |
| 28 | h |
| 29 | .handle("create", ({ payload }) => links.create(payload.url).pipe(Effect.orDie)) |
| 30 | .handle("get", ({ params }) => links.get(params.code).pipe(dieOnStore)) |
| 31 | .handle("list", () => links.list().pipe(Effect.orDie)), |
| 32 | ); |
| 33 | // #endregion handlers |
| 34 | const api = yield* serve(handlers); |
| 35 | |
| 36 | return { |
| 37 | // #region fetchTop |
| 38 | fetch: Effect.gen(function* () { |
| 39 | const request = yield* HttpServerRequest; |
| 40 | const [, code, action] = new URL(request.url).pathname.split("/"); |
| 41 | // #endregion fetchTop |
| 42 | // #region live |
| 43 | |
| 44 | if (action === "live") { |
| 45 | return yield* rooms.getByName(code!).fetch(request); |
| 46 | } |
| 47 | // #endregion live |
| 48 | // #region click |
| 49 | |
| 50 | if (code && code !== "links") { |
| 51 | const link = yield* links.get(code).pipe(dieOnStore); |
| 52 | yield* rooms.getByName(code).record(); |
| 53 | return HttpServerResponse.redirect(link.url, { status: 302 }); |
| 54 | } |
| 55 | // #endregion click |
| 56 | |
| 57 | // #region fetchEnd |
| 58 | return yield* api; |
| 59 | // #endregion fetchEnd |
| 60 | }).pipe( |
| 61 | Effect.catchTag("LinkNotFound", () => |
| 62 | Effect.succeed(HttpServerResponse.empty({ status: 404 })), |
| 63 | ), |
| 64 | ), |
| 65 | }; |
| 66 | }).pipe(Effect.provide(LinksSql.pipe(Layer.provide(NeonStorage)))), |
| 67 | ); |
| 68 | // #endregion show |