| 1 | import * as Cloudflare from "@/Cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | import ChatBackend from "./ChatBackend.ts"; |
| 6 | |
| 7 | export default class ChatPersistenceTestWorker extends Cloudflare.Worker<ChatPersistenceTestWorker>()( |
| 8 | "ChatPersistenceTestWorker", |
| 9 | { |
| 10 | main: import.meta.url, |
| 11 | }, |
| 12 | Effect.gen(function* () { |
| 13 | // Yielding the inline DO hosts it on this Worker and hands back the |
| 14 | // namespace handle. |
| 15 | const chats = yield* ChatBackend; |
| 16 | |
| 17 | return { |
| 18 | fetch: Effect.gen(function* () { |
| 19 | const request = yield* HttpServerRequest; |
| 20 | const url = new URL(request.url, "http://worker"); |
| 21 | const id = url.searchParams.get("id") ?? "default"; |
| 22 | const prompt = url.searchParams.get("prompt") ?? "Say pong."; |
| 23 | |
| 24 | if (url.pathname === "/chat") { |
| 25 | const result = yield* chats.getByName(id).send(id, prompt); |
| 26 | return yield* HttpServerResponse.json(result); |
| 27 | } |
| 28 | |
| 29 | return HttpServerResponse.text("ok"); |
| 30 | }), |
| 31 | }; |
| 32 | }), |
| 33 | ) {} |