import * as Cloudflare from "@alchemy.run/cloudflare"; import * as Fly from "@alchemy.run/fly"; import * as Neon from "@alchemy.run/neon"; import * as Alchemy from "alchemy"; import { Stage } from "alchemy"; import * as Config from "effect/Config"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Redacted from "effect/Redacted"; import { flyAppName } from "everything-dev/hosting/fly"; const account = Config.String("BOS_ACCOUNT"); const gateway = Config.String("BOS_GATEWAY"); const image = Config.String("BOS_IMAGE"); const authSecret = Config.Redacted("BETTER_AUTH_SECRET"); const stageRegion = Config.String("FLY_REGION").pipe(Config.withDefault("iad")); const publicBaseUrl = Config.String("BASE_URL").pipe(Config.withDefault("")); const dataMode = Config.String("BOS_STAGE_DATA").pipe(Config.withDefault("pglite")); const neonOrgId = Config.String("NEON_ORG_ID"); export default Alchemy.Stack( "BosStages", { providers: Layer.mergeAll(Fly.providers(), Neon.providers(), Cloudflare.providers()), state: Alchemy.localState(), }, Effect.gen(function* () { const bootAccount = yield* account; const bootGateway = yield* gateway; const bootImage = yield* image; const secret = yield* authSecret; const region = yield* stageRegion; const data = yield* dataMode; const stage = yield* Stage; const port = 4100; const app = yield* Fly.App("Host", { name: flyAppName(stage) }); const publicIp = yield* Fly.IpAssignment("Public", { app, type: "shared_v4" }); const baseUrl = (yield* publicBaseUrl) || app.url; const domain = new URL(baseUrl).hostname; const ownZone = (yield* bootGateway).replace(/^\.|\$/g, ""); const underOwnZone = domain === ownZone || domain.endsWith(`.${ownZone}`); if (underOwnZone) { const dns = yield* DnsAdapter.resolve(Cloudflare.DNS.Adapter()); yield* dns.alias("Dns", { name: domain, target: { ipv4: [publicIp.ip] } }); } yield* Fly.Certificate("Cert", { app, hostname: domain, kind: "acme" }); const machineEnv: Record = { BOS_ACCOUNT: bootAccount, BOS_GATEWAY: bootGateway, BETTER_AUTH_SECRET: Redacted.value(secret), PORT: String(port), }; if (data === "neon") { const orgId = yield* neonOrgId; const project = yield* Neon.Project("Data", { name: `${flyAppName(stage)}-db`, orgId, historyRetentionSeconds: 21600, }); const apiBranch = yield* Neon.Branch("Api", { project }); const authBranch = yield* Neon.Branch("Auth", { project }); machineEnv.API_DATABASE_URL = apiBranch.connectionUri; machineEnv.AUTH_DATABASE_URL = authBranch.connectionUri; } const machine = yield* Fly.Machine("Runtime", { app, region, image: bootImage, env: machineEnv, guest: { cpuKind: "shared", cpus: 2, memoryMb: 2048 }, services: [ { protocol: "tcp", internalPort: port, ports: [ { port: 80, handlers: ["http"], forceHttps: true }, { port: 443, handlers: ["tls", "http"] }, ], checks: [ { type: "http", port, path: "/health", interval: "10s", timeout: "2s", gracePeriod: "30s", }, ], }, ], deploy: { strategy: "bluegreen", healthTimeout: "300 seconds" }, }); return { url: app.url, appName: app.appName, machineId: machine.machineId }; }), );