The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Drizzle from "@alchemy.run/drizzle/Postgres"; |
| 2 | import * as GCP from "@alchemy.run/gcp"; |
| 3 | import { eq } from "drizzle-orm"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Schema from "effect/Schema"; |
| 8 | import { database } from "./database.ts"; |
| 9 | import { todos } from "./schema.ts"; |
| 10 | |
| 11 | const Id = Schema.String.check(Schema.isUUID()); |
| 12 | |
| 13 | /** |
| 14 | * A todo API on Cloud Run backed by Cloud SQL for PostgreSQL. |
| 15 | * |
| 16 | * `GCP.SQL.Connect` mounts the instance's Unix socket into the revision |
| 17 | * and grants the runtime service account `roles/cloudsql.client` on this |
| 18 | * instance only; Drizzle then talks Postgres over |
| 19 | * `/cloudsql/{connectionName}`. Binding the database makes the service |
| 20 | * deploy after its migrations are applied. |
| 21 | */ |
| 22 | export default class Api extends GCP.Function<Api>()( |
| 23 | "Api", |
| 24 | Effect.gen(function* () { |
| 25 | const { schema } = yield* database; |
| 26 | return { |
| 27 | main: import.meta.url, |
| 28 | invokerIamDisabled: true, |
| 29 | // Rolls a new revision whenever the schema changes. |
| 30 | env: { SCHEMA_VERSION: schema.snapshotHash }, |
| 31 | }; |
| 32 | }), |
| 33 | Effect.gen(function* () { |
| 34 | const { instance, db: appDb, user, passwordSecret } = yield* database; |
| 35 | const connect = yield* GCP.SQL.Connect(instance, { |
| 36 | database: appDb, |
| 37 | user, |
| 38 | passwordSecret, |
| 39 | }); |
| 40 | const db = yield* Drizzle.Postgres(connect.pipe(Effect.map((info) => info.url))); |
| 41 | |
| 42 | return { |
| 43 | fetch: Effect.gen(function* () { |
| 44 | const request = yield* HttpServerRequest; |
| 45 | const url = new URL(request.originalUrl, "http://localhost"); |
| 46 | |
| 47 | if (request.method === "GET" && url.pathname === "/") { |
| 48 | return HttpServerResponse.text("ok"); |
| 49 | } |
| 50 | if (url.pathname === "/todos" && request.method === "GET") { |
| 51 | return yield* HttpServerResponse.json( |
| 52 | yield* db.select().from(todos).orderBy(todos.title), |
| 53 | ); |
| 54 | } |
| 55 | if (url.pathname === "/todos" && request.method === "POST") { |
| 56 | const input = yield* request.json.pipe( |
| 57 | Effect.flatMap( |
| 58 | Schema.decodeUnknownEffect(Schema.Struct({ id: Id, title: Schema.NonEmptyString })), |
| 59 | ), |
| 60 | ); |
| 61 | return yield* HttpServerResponse.json(yield* db.insert(todos).values(input).returning(), { |
| 62 | status: 201, |
| 63 | }); |
| 64 | } |
| 65 | if (url.pathname.startsWith("/todos/")) { |
| 66 | const id = yield* Schema.decodeUnknownEffect(Id)(url.pathname.slice("/todos/".length)); |
| 67 | if (request.method === "GET") { |
| 68 | const [todo] = yield* db.select().from(todos).where(eq(todos.id, id)); |
| 69 | return todo |
| 70 | ? yield* HttpServerResponse.json(todo) |
| 71 | : HttpServerResponse.text("Not found", { status: 404 }); |
| 72 | } |
| 73 | if (request.method === "PATCH") { |
| 74 | const input = yield* request.json.pipe( |
| 75 | Effect.flatMap(Schema.decodeUnknownEffect(Schema.Struct({ done: Schema.Boolean }))), |
| 76 | ); |
| 77 | const [todo] = yield* db.update(todos).set(input).where(eq(todos.id, id)).returning(); |
| 78 | return todo |
| 79 | ? yield* HttpServerResponse.json(todo) |
| 80 | : HttpServerResponse.text("Not found", { status: 404 }); |
| 81 | } |
| 82 | if (request.method === "DELETE") { |
| 83 | yield* db.delete(todos).where(eq(todos.id, id)); |
| 84 | return HttpServerResponse.empty({ status: 204 }); |
| 85 | } |
| 86 | } |
| 87 | return HttpServerResponse.text("Not found", { status: 404 }); |
| 88 | }).pipe( |
| 89 | Effect.catchTag("SchemaError", () => |
| 90 | Effect.succeed(HttpServerResponse.text("Invalid request", { status: 400 })), |
| 91 | ), |
| 92 | Effect.orDie, |
| 93 | ), |
| 94 | }; |
| 95 | }).pipe(Effect.provide(GCP.SQL.ConnectHttp)), |
| 96 | ) {} |