The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | # @alchemy.run/git |
| 2 | |
| 3 | A pluggable, self-hostable git server for [Alchemy](https://alchemy.run), built on Cloudflare Workers, Durable Objects, and R2. It serves smart HTTP to any git client, plus a typed REST API with pull requests and a GitHub-compatible surface. |
| 4 | |
| 5 | ```sh |
| 6 | npm install alchemy @alchemy.run/git @alchemy.run/cloudflare effect |
| 7 | ``` |
| 8 | |
| 9 | A git host behind one shared secret: |
| 10 | |
| 11 | ```typescript |
| 12 | // src/git.ts |
| 13 | import * as Alchemy from "alchemy"; |
| 14 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 15 | import * as Git from "@alchemy.run/git"; |
| 16 | import * as Http from "alchemy/Http"; |
| 17 | import * as Effect from "effect/Effect"; |
| 18 | import * as HttpRouter from "effect/http/HttpRouter"; |
| 19 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 20 | import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder"; |
| 21 | import * as HttpApiSecurity from "effect/http-api/HttpApiSecurity"; |
| 22 | import * as Layer from "effect/Layer"; |
| 23 | import * as Redacted from "effect/Redacted"; |
| 24 | |
| 25 | export const GitObjects = Cloudflare.R2.Bucket("GitObjects"); |
| 26 | export const GitSecret = Effect.gen(function* () { |
| 27 | const Random = yield* Alchemy.Random; |
| 28 | return yield* Random("GitSecret"); |
| 29 | }); |
| 30 | |
| 31 | /** The shared secret, sent as the HTTP Basic password. */ |
| 32 | export const Authentication = HttpRouter.middleware( |
| 33 | Effect.gen(function* () { |
| 34 | const secret = yield* (yield* GitSecret).text; |
| 35 | return (httpEffect) => |
| 36 | Effect.gen(function* () { |
| 37 | const { password } = yield* HttpApiBuilder.securityDecode(HttpApiSecurity.basic); |
| 38 | if (Redacted.value(password) === Redacted.value(yield* secret)) { |
| 39 | return yield* httpEffect; |
| 40 | } |
| 41 | return HttpServerResponse.empty({ |
| 42 | status: 401, |
| 43 | headers: { "www-authenticate": 'Basic realm="git"' }, |
| 44 | }); |
| 45 | }).pipe(Effect.provide(Alchemy.RuntimeContext.phantom)); |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | const GitLive = Layer.mergeAll( |
| 50 | Git.ApiLive.pipe(Layer.provide(Authentication.layer)), |
| 51 | Git.InternalApiLive, |
| 52 | ).pipe( |
| 53 | Layer.provide(Git.ApiHandlersLive), |
| 54 | Layer.provide(Git.ReposDurableObject), // refs, objects, pull requests |
| 55 | Layer.provide(Git.RegistryDurableObject), // owner/name → repo |
| 56 | Layer.provide(Git.HasherInline), // push verification |
| 57 | Layer.provide(Git.BlobStoreR2(GitObjects)), // packs and large pushes |
| 58 | Layer.provide(Http.Platform), |
| 59 | ); |
| 60 | |
| 61 | export default class GitHost extends Cloudflare.Worker<GitHost>()( |
| 62 | "Git", |
| 63 | { main: import.meta.url, ...Git.GIT_WORKER_OPTIONS }, |
| 64 | Effect.gen(function* () { |
| 65 | const fetch = yield* HttpRouter.toHttpEffect(GitLive); |
| 66 | return { fetch }; |
| 67 | }).pipe(Effect.provide(Cloudflare.R2.ReadWriteBucketBinding)), |
| 68 | ) {} |
| 69 | ``` |
| 70 | |
| 71 | ```typescript |
| 72 | // alchemy.run.ts |
| 73 | import * as Alchemy from "alchemy"; |
| 74 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 75 | import * as Output from "alchemy/Output"; |
| 76 | import * as Effect from "effect/Effect"; |
| 77 | import * as Redacted from "effect/Redacted"; |
| 78 | import GitHost, { GitSecret } from "./src/git.ts"; |
| 79 | |
| 80 | export default Alchemy.Stack( |
| 81 | "GitService", |
| 82 | { providers: Cloudflare.providers(), state: Cloudflare.state() }, |
| 83 | Effect.gen(function* () { |
| 84 | const git = yield* GitHost; |
| 85 | const secret = yield* GitSecret; |
| 86 | return { |
| 87 | url: git.url.as<string>(), |
| 88 | secret: Output.map(secret.text, Redacted.value), |
| 89 | }; |
| 90 | }), |
| 91 | ); |
| 92 | ``` |
| 93 | |
| 94 | After `alchemy deploy`, `git clone` and `git push` work against the printed URL. |
| 95 | |
| 96 | ## Learn more |
| 97 | |
| 98 | - [Git on alchemy.run](https://alchemy.run/git) |
| 99 | - [Getting started](https://alchemy.run/git/getting-started) |
| 100 | - [Tutorial](https://alchemy.run/git/tutorial/part-1) |