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 Fly from "@alchemy.run/fly"; |
| 3 | import { eq, sql } 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 { relations, Users } from "./schema.ts"; |
| 8 | import { API_PORT, Db, MIGRATE_TOKEN } from "./shared.ts"; |
| 9 | |
| 10 | /** |
| 11 | * HTTP Service that binds Managed Postgres via {@link Fly.ConnectPostgres} |
| 12 | * and queries through Drizzle. |
| 13 | * |
| 14 | * Also exposes `POST /migrate`: MPG is only reachable on the org's private |
| 15 | * network, so DDL is applied through this in-network Service — the deploy |
| 16 | * (or test) posts the generated migration SQL here instead of connecting |
| 17 | * to the cluster directly. |
| 18 | */ |
| 19 | export default class Api extends Fly.Service<Api>()( |
| 20 | "Api", |
| 21 | { |
| 22 | main: import.meta.url, |
| 23 | region: "iad", |
| 24 | port: API_PORT, |
| 25 | guest: { cpuKind: "shared", cpus: 1, memoryMb: 256 }, |
| 26 | }, |
| 27 | Effect.gen(function* () { |
| 28 | const conn = yield* Fly.ConnectPostgres(Db); |
| 29 | const db = yield* Drizzle.Postgres(conn.connectionString, { |
| 30 | relations, |
| 31 | }); |
| 32 | // DDL and other session-scoped statements need the direct |
| 33 | // (non-PgBouncer) connection. |
| 34 | const directDb = yield* Drizzle.Postgres(conn.directConnectionString); |
| 35 | |
| 36 | return { |
| 37 | fetch: Effect.gen(function* () { |
| 38 | const request = yield* HttpServerRequest; |
| 39 | const path = new URL(request.url, "http://service").pathname; |
| 40 | if (path === "/migrate" && request.method === "POST") { |
| 41 | if (request.headers["x-migrate-token"] !== MIGRATE_TOKEN) { |
| 42 | return yield* HttpServerResponse.json({ error: "unauthorized" }, { status: 401 }); |
| 43 | } |
| 44 | // drizzle-kit emits one file per migration with statements |
| 45 | // separated by `--> statement-breakpoint`. Apply each statement, |
| 46 | // tolerating re-runs (the client retries through service warmup, |
| 47 | // so a statement may already have been applied). |
| 48 | const body = yield* request.text; |
| 49 | const statements = body |
| 50 | .split("--> statement-breakpoint") |
| 51 | .map((statement) => statement.trim()) |
| 52 | .filter((statement) => statement.length > 0); |
| 53 | for (const statement of statements) { |
| 54 | yield* directDb |
| 55 | .execute(sql.raw(statement)) |
| 56 | .pipe( |
| 57 | Effect.catch((error) => |
| 58 | String(error).includes("already exists") ? Effect.void : Effect.fail(error), |
| 59 | ), |
| 60 | ); |
| 61 | } |
| 62 | return yield* HttpServerResponse.json({ |
| 63 | applied: statements.length, |
| 64 | }); |
| 65 | } |
| 66 | if (path === "/health") { |
| 67 | const users = yield* db.select().from(Users); |
| 68 | return yield* HttpServerResponse.json({ |
| 69 | ok: true, |
| 70 | users: users.length, |
| 71 | }); |
| 72 | } |
| 73 | switch (request.method) { |
| 74 | case "GET": { |
| 75 | if (path === "/" || path === "/users") { |
| 76 | const users = yield* db.select().from(Users); |
| 77 | return yield* HttpServerResponse.json({ users }); |
| 78 | } |
| 79 | const id = Number(path.split("/").pop()); |
| 80 | if (Number.isNaN(id)) { |
| 81 | return yield* HttpServerResponse.json({ error: "Invalid user ID" }, { status: 400 }); |
| 82 | } |
| 83 | const user = yield* db.query.Users.findFirst({ |
| 84 | where: { id }, |
| 85 | with: { posts: true }, |
| 86 | }); |
| 87 | return yield* HttpServerResponse.json({ user }); |
| 88 | } |
| 89 | case "POST": { |
| 90 | const user = yield* db |
| 91 | .insert(Users) |
| 92 | .values({ |
| 93 | name: crypto.randomUUID(), |
| 94 | email: crypto.randomUUID(), |
| 95 | }) |
| 96 | .returning(); |
| 97 | return yield* HttpServerResponse.json({ user }); |
| 98 | } |
| 99 | case "DELETE": { |
| 100 | const id = Number(path.split("/").pop()); |
| 101 | if (Number.isNaN(id)) { |
| 102 | return yield* HttpServerResponse.json({ error: "Invalid user ID" }, { status: 400 }); |
| 103 | } |
| 104 | const [user] = yield* db.delete(Users).where(eq(Users.id, id)).returning(); |
| 105 | return yield* HttpServerResponse.json({ user }); |
| 106 | } |
| 107 | default: { |
| 108 | return yield* HttpServerResponse.json({ error: "Method not allowed" }, { status: 405 }); |
| 109 | } |
| 110 | } |
| 111 | }).pipe( |
| 112 | Effect.catch((cause: unknown) => |
| 113 | HttpServerResponse.json({ ok: false, error: String(cause) }, { status: 500 }), |
| 114 | ), |
| 115 | ), |
| 116 | }; |
| 117 | }).pipe(Effect.provide(Fly.ConnectPostgresHttp)), |
| 118 | ) {} |