flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/deploy/self-host/configs.mjs

218 lines7,996 bytesCodeBlame
1#!/usr/bin/env node
2// Writes the Wrangler configs a self-hosted g1t runs with, derived from the
3// hosted ones, so the two never drift apart.
4//
5// Each hosted service's wrangler.jsonc is read and changed only where
6// Cloudflare-only things live:
7//
8// - account, routes, placement, observability and builds are dropped;
9// - ARTIFACTS (git storage) becomes a service binding to workers/artifacts,
10// which keeps repositories in the git store (gitstore/server.mjs);
11// - EMAIL (Email Sending) becomes a service binding to workers/mail;
12// - services that are off in this phase (agents, the context hub, the
13// g1t.page dispatcher, model proxy) are bound to workers/off instead, and
14// events stop queueing work for them;
15// - URLs that name g1t.sh name PUBLIC_URL instead, and billing is free.
16//
17// Usage: node configs.mjs [outDir]
18// Environment: PUBLIC_URL, GITSTORE_URL, GITSTORE_SECRET, MAIL_URL,
19// ACTIONS_KEY, INTEGRATIONS_KEY, WEBHOOKS_KEY.
20//
21// The output is for `wrangler dev` (see start.sh): every Worker in one
22// workerd, the site first, with D1, KV and Queues kept on disk.
23
24import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
25import { dirname, join, relative, resolve } from "node:path";
26import { fileURLToPath } from "node:url";
27
28const here = dirname(fileURLToPath(import.meta.url));
29const root = resolve(here, "../..");
30const out = resolve(process.argv[2] ?? join(here, ".generated"));
31mkdirSync(out, { recursive: true });
32
33const PUBLIC_URL = (process.env.PUBLIC_URL ?? "http://localhost:8787").replace(/\/$/, "");
34
35/** Services that run, in the order Wrangler is given them (the site first). */
36export const RUNNING = [
37 { name: "g1t", dir: "apps/web", web: true },
38 { name: "g1t-identity", dir: "services/identity" },
39 { name: "g1t-repos", dir: "services/repos" },
40 { name: "g1t-work", dir: "services/work" },
41 { name: "g1t-events", dir: "services/events" },
42 { name: "g1t-projects", dir: "services/projects" },
43 { name: "g1t-search", dir: "services/search" },
44 { name: "g1t-billing", dir: "services/billing" },
45 { name: "g1t-security", dir: "services/security" },
46 { name: "g1t-actions", dir: "services/actions" },
47 { name: "g1t-webhooks", dir: "services/webhooks" },
48 { name: "g1t-integrations", dir: "services/integrations" },
49 { name: "g1t-deployments", dir: "services/deployments" },
50];
51
52/** Services that are off in phase 1, and what the off Worker calls them. */
53const OFF = {
54 "g1t-runner": "Agents",
55 "g1t-context": "Context search and memory",
56};
57
58/** Sealing keys, by the service that holds each (hosted: Wrangler secrets). */
59const SECRETS = {
60 "g1t-actions": "ACTIONS_KEY",
61 "g1t-integrations": "INTEGRATIONS_KEY",
62 "g1t-webhooks": "WEBHOOKS_KEY",
63};
64
65/** Queues whose consumers are off: events stops sending to them. */
66const OFF_QUEUES = new Set(["g1t-events-runner", "g1t-events-context"]);
67
68/** Strips comments and trailing commas from JSONC. Strings are respected. */
69function parseJsonc(text) {
70 let result = "";
71 let inString = false;
72 for (let i = 0; i < text.length; i++) {
73 const char = text[i];
74 if (inString) {
75 result += char;
76 if (char === "\\") result += text[++i];
77 else if (char === '"') inString = false;
78 } else if (char === '"') {
79 inString = true;
80 result += char;
81 } else if (char === "/" && text[i + 1] === "/") {
82 while (i < text.length && text[i] !== "\n") i++;
83 result += "\n";
84 } else if (char === "/" && text[i + 1] === "*") {
85 i = text.indexOf("*/", i + 2) + 1;
86 } else {
87 result += char;
88 }
89 }
90 return JSON.parse(result.replace(/,(\s*[}\]])/g, "$1"));
91}
92
93const rel = (path) => relative(out, resolve(root, path)).replaceAll("\\", "/");
94
95function hostedUrl(value) {
96 return typeof value === "string" ? value.replace(/https:\/\/(api\.)?g1t\.sh/g, PUBLIC_URL) : value;
97}
98
99function selfHosted(service) {
100 const hosted = parseJsonc(readFileSync(join(root, service.dir, "wrangler.jsonc"), "utf8"));
101 const config = {
102 name: hosted.name,
103 compatibility_date: hosted.compatibility_date,
104 compatibility_flags: hosted.compatibility_flags,
105 rules: hosted.rules,
106 vars: {},
107 };
108
109 if (service.web) {
110 // The site as React Router built it (apps/web/build), not its sources.
111 config.main = rel(`${service.dir}/build/server/index.js`);
112 config.no_bundle = true;
113 config.rules = [{ type: "ESModule", globs: ["**/*.js", "**/*.mjs"] }];
114 config.assets = { directory: rel(`${service.dir}/build/client`) };
115 } else {
116 config.main = rel(join(service.dir, hosted.main));
117 }
118
119 for (const [key, value] of Object.entries(hosted.vars ?? {})) config.vars[key] = hostedUrl(value);
120
121 if (hosted.d1_databases) {
122 config.d1_databases = hosted.d1_databases.map((db) => ({
123 binding: db.binding,
124 database_name: db.database_name,
125 database_id: db.database_id,
126 migrations_dir: rel(join(service.dir, db.migrations_dir ?? "migrations")),
127 }));
128 }
129 if (hosted.kv_namespaces) config.kv_namespaces = hosted.kv_namespaces.map(({ binding, id }) => ({ binding, id }));
130 if (hosted.triggers) config.triggers = hosted.triggers;
131
132 if (hosted.queues) {
133 config.queues = {};
134 if (hosted.queues.producers) {
135 config.queues.producers = hosted.queues.producers.filter((producer) => !OFF_QUEUES.has(producer.queue));
136 }
137 if (hosted.queues.consumers) config.queues.consumers = hosted.queues.consumers;
138 }
139
140 config.services = (hosted.services ?? []).map((binding) =>
141 OFF[binding.service] ? { binding: binding.binding, service: offName(binding.service) } : binding,
142 );
143
144 // Cloudflare-only bindings, and what stands in for them.
145 if (hosted.artifacts) {
146 for (const artifacts of hosted.artifacts) {
147 config.services.push({ binding: artifacts.binding, service: "g1t-artifacts" });
148 }
149 }
150 if (hosted.send_email) {
151 for (const email of hosted.send_email) config.services.push({ binding: email.name, service: "g1t-mail" });
152 }
153
154 // Secrets the hosted services hold, given here from the environment, each
155 // only to the service that uses it.
156 const secret = SECRETS[hosted.name];
157 if (secret && process.env[secret]) config.vars[secret] = process.env[secret];
158
159 // Self-hosted g1t charges nothing: billing records usage at cost and never
160 // stops work for it.
161 if (hosted.name === "g1t-billing") config.vars.FREE_WHILE_BUILDING = "true";
162 // Nothing to deploy to: deployments are off (no Cloudflare API token).
163 if (hosted.name === "g1t-deployments") delete config.vars.CUSTOM_HOSTNAMES_ZONE_ID;
164
165 return config;
166}
167
168function offName(service) {
169 return `${service}-off`;
170}
171
172function write(name, config) {
173 const path = join(out, `${name}.json`);
174 writeFileSync(path, `${JSON.stringify(config, null, 2)}\n`);
175 return path;
176}
177
178const files = [];
179for (const service of RUNNING) files.push(write(service.name, selfHosted(service)));
180
181const compatibility_date = "2026-09-26";
182files.push(
183 write("g1t-artifacts", {
184 name: "g1t-artifacts",
185 main: rel("deploy/self-host/workers/artifacts/index.js"),
186 compatibility_date,
187 vars: {
188 GITSTORE_URL: process.env.GITSTORE_URL ?? "http://gitstore:8080",
189 GITSTORE_SECRET: process.env.GITSTORE_SECRET ?? "",
190 },
191 }),
192);
193files.push(
194 write("g1t-mail", {
195 name: "g1t-mail",
196 main: rel("deploy/self-host/workers/mail/index.js"),
197 compatibility_date,
198 vars: {
199 PUBLIC_URL,
200 MAIL_URL: process.env.MAIL_URL ?? "",
201 MAIL_FROM: process.env.MAIL_FROM ?? "",
202 },
203 }),
204);
205for (const [service, feature] of Object.entries(OFF)) {
206 files.push(
207 write(offName(service), {
208 name: offName(service),
209 main: rel("deploy/self-host/workers/off/index.js"),
210 compatibility_date,
211 vars: { OFF_NAME: feature },
212 }),
213 );
214}
215
216// The order Wrangler takes them in: the site first, as the one that serves.
217writeFileSync(join(out, "workers.txt"), `${files.map((file) => relative(out, file)).join("\n")}\n`);
218console.log(`Wrote ${files.length} configs to ${out}`);