summaryrefslogtreecommitdiffstats
path: root/GIT_AND_GOMOD_PLAN.md
blob: 183154fe8f338f7830834b12c6d9a3ef50925526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Go Vanity Import + Git Server Architecture

This document describes a **simple, robust architecture** for hosting Go module source code and enabling `go get` on a **custom domain**, using:

* A basic Git server (authoritative source)
* Go vanity imports
* Caddy for HTTPS with automatic TLS

The design prioritizes:

* Simplicity
* Long-term correctness
* Zero client-side configuration
* Easy future migration to a module proxy if needed

---

## Goals

* Host all authored Go modules under a single domain (e.g. `yourdomain.com/foo`)
* Allow anyone to run:

  ```bash
  go get yourdomain.com/foo
  ```

  with no environment variables or flags
* Retain full control of source code and hosting
* Avoid unnecessary complexity (S3 proxies, CI artifact pipelines) for a single author

---

## High-Level Architecture

```
yourdomain.com
 ├─ /<module>            → Vanity import discovery (?go-get=1)
 ├─ /<module>.git        → Git repository (HTTPS, read-only)
 └─ ssh://git@yourdomain.com/<module>.git → Git writes (SSH)
```

Components:

* **Caddy**: Fronts everything, provides HTTPS automatically
* **Vanity Import Server**: Tiny Go HTTP server that serves `<meta go-import>` tags
* **Git Server**: Bare Git repositories served over HTTPS and SSH

---

## How `go get` Works in This Setup

When a user runs:

```bash
go get yourdomain.com/foo
```

Go performs the following steps:

1. **Vanity discovery**

   ```
   GET https://yourdomain.com/foo?go-get=1
   ```

2. **Vanity server responds** with:

   ```html
   <meta name="go-import"
         content="yourdomain.com/foo git https://yourdomain.com/foo.git">
   ```

3. **Go clones the repository**:

   ```
   git clone https://yourdomain.com/foo.git
   ```

4. **Go reads `go.mod`**, checks tags (e.g. `v1.2.3`), and builds the module

No Go module proxy is involved.

---

## Module Requirements

Each module repository **must**:

* Be located at `/srv/git/<module>.git`
* Have a `go.mod` file with:

  ```go
  module yourdomain.com/<module>
  ```
* Use semantic version tags:

  ```
  v1.0.0
  v1.2.3
  ```

Tags must not be rewritten after publication.

---

## Vanity Import Server

A single Go HTTP server can serve vanity imports for **all modules**.

### Responsibilities

* Respond only to `?go-get=1` requests
* Dynamically map paths to Git repositories
* Serve static HTML with `<meta name="go-import">`

### Behavior

For a request to:

```
/foo?go-get=1
```

The server returns:

```html
<meta name="go-import"
      content="yourdomain.com/foo git https://yourdomain.com/foo.git">
```

All non-`go-get` requests return `404` (or are handled elsewhere).

---

## Git Server

### Repository Layout

```
/srv/git/
 ├─ foo.git
 ├─ bar.git
 └─ baz.git
```

Repositories are:

* **Bare**
* Read-only over HTTPS
* Writable only via SSH

### Write Access

* SSH only
* Single `git` user
* Access controlled via `authorized_keys`

Example push:

```bash
git push git@yourdomain.com:/srv/git/foo.git
```

---

## Caddy Configuration

Caddy is used as the front-facing server.

### Responsibilities

* Automatic HTTPS (Let’s Encrypt)
* Route `.git` paths to `git-http-backend`
* Route all other paths to the vanity import server

### Conceptual Routing

* `/*.git*` → Git HTTP backend
* `/*` → Vanity import server

This keeps TLS, routing, and process management simple and centralized.

---

## Why This Design Is “Bullet-Proof Simple”

### Advantages

* No module proxy implementation required
* No S3, CloudFront, or artifact format rules
* No CI publishing pipeline needed
* Fully compatible with Go tooling
* Easy to debug (if `git clone` works, `go get` works)
* Mirrors Go’s original and still-supported design

### Trade-offs

* Git-based fetching is slower than a proxy
* No immutable release guarantees beyond Git tags
* Less suitable for very high traffic

For a single-author, self-hosted setup, these trade-offs are acceptable.

---

## Future Migration Path (Optional)

If needed later, this setup can evolve into a module proxy:

* Keep vanity import paths unchanged
* Switch `<meta go-import>` from `git` → `mod`
* Introduce an S3-backed module proxy
* Users do not need to change imports or commands

This design does not lock you into Git forever.

---

## Summary

This architecture provides:

* A single, authoritative domain for all Go modules
* Simple Git-based source hosting
* Zero-config `go get` for users
* Minimal operational complexity
* A clean upgrade path to a full module proxy

It is the simplest solution that is still fully correct and future-proof for Go modules.