The best way to control Claude Code, Codex and more from any computer or phone
| 1 | --- |
| 2 | title: All on Cloudflare |
| 3 | description: A git host on one Cloudflare account. One Worker, one bucket, two Durable Object namespaces, and dynamically loaded Workers hashing large pushes. |
| 4 | --- |
| 5 | |
| 6 | The examples use `Authentication` from |
| 7 | [Getting Started](/git/getting-started): the application's request middleware. |
| 8 | |
| 9 | import PackageManagers from "../../../../components/PackageManagers.astro"; |
| 10 | |
| 11 | Everything on one account, with large pushes hashed by dynamically |
| 12 | loaded Workers so nothing leaves Cloudflare: |
| 13 | |
| 14 | ```typescript |
| 15 | // src/git.ts |
| 16 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 17 | import * as Git from "@alchemy.run/git"; |
| 18 | import * as GitHasher from "@alchemy.run/git/Hasher"; |
| 19 | import * as Effect from "effect/Effect"; |
| 20 | import * as Layer from "effect/Layer"; |
| 21 | import * as HttpRouter from "effect/http/HttpRouter"; |
| 22 | import * as Http from "alchemy/Http"; |
| 23 | |
| 24 | export const GitObjects = Cloudflare.R2.Bucket("GitObjects"); |
| 25 | |
| 26 | const GitLive = Git.ApiLive.pipe( |
| 27 | Layer.provide(Git.ApiHandlersLive), |
| 28 | Layer.provide(Authentication.layer), |
| 29 | Layer.provide(Git.ReposDurableObject), |
| 30 | Layer.provide(Git.RegistryDurableObject), |
| 31 | Layer.provide(GitHasher.HasherWorkerLoader()), |
| 32 | Layer.provide(Git.BlobStoreR2(GitObjects)), |
| 33 | ); |
| 34 | |
| 35 | export default class GitHost extends Cloudflare.Worker<GitHost>()( |
| 36 | "Git", |
| 37 | { main: import.meta.url, ...Git.GIT_WORKER_OPTIONS }, |
| 38 | Effect.gen(function* () { |
| 39 | const fetch = yield* HttpRouter.toHttpEffect(GitLive.pipe(Layer.provide(Http.Platform))); |
| 40 | return { fetch }; |
| 41 | }).pipe(Effect.provide(Cloudflare.R2.ReadWriteBucketBinding)), |
| 42 | ) {} |
| 43 | ``` |
| 44 | |
| 45 | ```typescript |
| 46 | // alchemy.run.ts |
| 47 | import * as Alchemy from "alchemy"; |
| 48 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 49 | import * as Effect from "effect/Effect"; |
| 50 | import GitHost from "./src/git.ts"; |
| 51 | |
| 52 | export default Alchemy.Stack( |
| 53 | "GitService", |
| 54 | { providers: Cloudflare.providers(), state: Cloudflare.state() }, |
| 55 | Effect.gen(function* () { |
| 56 | const git = yield* GitHost; |
| 57 | return { url: git.url.as<string>() }; |
| 58 | }), |
| 59 | ); |
| 60 | ``` |
| 61 | |
| 62 | <PackageManagers run="alchemy deploy" /> |
| 63 | |
| 64 | ## What gets created |
| 65 | |
| 66 | - An R2 bucket for packs, clone bundles, and spilled push bodies. |
| 67 | - A Worker with `nodejs_compat`, a 300 s CPU limit, a `worker_loader` |
| 68 | binding for the hasher, and the shared secret bound as a secret. |
| 69 | - Two Durable Object namespaces on that Worker, one per repository and |
| 70 | one for the registry, each with SQLite storage. |
| 71 | |
| 72 | Nothing runs until a request arrives, and a repository that receives |
| 73 | no traffic costs its storage. |
| 74 | |
| 75 | ## Variations |
| 76 | |
| 77 | `HasherInline` is enough until pushes of tens of megabytes matter: |
| 78 | |
| 79 | ```diff lang="typescript" |
| 80 | const GitLive = Git.ApiLive.pipe( |
| 81 | Layer.provide(Git.ApiHandlersLive), |
| 82 | Layer.provide(Authentication.layer), |
| 83 | Layer.provide(Git.ReposDurableObject), |
| 84 | Layer.provide(Git.RegistryDurableObject), |
| 85 | - Layer.provide(GitHasher.HasherWorkerLoader()), |
| 86 | + Layer.provide(Git.HasherInline), |
| 87 | Layer.provide(Git.BlobStoreR2(GitObjects)), |
| 88 | ); |
| 89 | ``` |
| 90 | |
| 91 | A registry on D1 replicates name lookups instead of serving them from |
| 92 | one region: |
| 93 | |
| 94 | ```diff lang="typescript" |
| 95 | +export const RepoIndex = Cloudflare.D1.Database("RepoIndex"); |
| 96 | + |
| 97 | const GitLive = Git.ApiLive.pipe( |
| 98 | Layer.provide(Git.ApiHandlersLive), |
| 99 | Layer.provide(Authentication.layer), |
| 100 | Layer.provide(Git.ReposDurableObject), |
| 101 | - Layer.provide(Git.RegistryDurableObject), |
| 102 | + Layer.provide(Git.RegistryD1(RepoIndex)), |
| 103 | Layer.provide(GitHasher.HasherWorkerLoader()), |
| 104 | Layer.provide(Git.BlobStoreR2(GitObjects)), |
| 105 | ); |
| 106 | ``` |
| 107 | |
| 108 | A custom domain is a Worker option, so clone URLs read |
| 109 | `https://git.example.com/acme/web.git`. [Custom domains & |
| 110 | routes](/cloudflare/networking/custom-domains) covers it, and |
| 111 | the [application example](https://github.com/alchemy-run/alchemy/tree/main/examples/cloudflare-git-service) |
| 112 | serves a browser UI and Git on the same origin. |