upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/docs/archive/2025-11-04-evening/architecture-diagram.md
blob: 056e5513ab33fbabb91cfc18304b3ff1d10cf2a5 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# ngit-grasp Architecture Diagram

**Date:** November 4, 2025  
**Purpose:** Visual reference for single-port architecture

---

## Current Architecture (WRONG ❌)

```
┌─────────────────────────────────────────┐
│  Port 8080                              │
│  ┌───────────────────────────────────┐  │
│  │   Nostr Relay (WebSocket)         │  │
│  │   - NIP-01 protocol               │  │
│  │   - Event storage                 │  │
│  │   - Subscriptions                 │  │
│  └───────────────────────────────────┘  │
└─────────────────────────────────────────┘

┌─────────────────────────────────────────┐
│  Port 8081 (WRONG!)                     │
│  ┌───────────────────────────────────┐  │
│  │   Git HTTP Server                 │  │
│  │   - Not implemented               │  │
│  └───────────────────────────────────┘  │
└─────────────────────────────────────────┘
```

**Problem:** GRASP-01 requires single port!

---

## Target Architecture (CORRECT ✅)

```
┌─────────────────────────────────────────────────────────────┐
│  Single Port (8080)                                         │
│                                                             │
│  ┌───────────────────────────────────────────────────────┐ │
│  │  actix-web HTTP Server                                │ │
│  │                                                         │ │
│  │  ┌──────────────────────────────────────────────────┐ │ │
│  │  │  CORS Middleware (ALL requests)                  │ │ │
│  │  │  - Access-Control-Allow-Origin: *                │ │ │
│  │  │  - Access-Control-Allow-Methods: GET, POST       │ │ │
│  │  │  - Access-Control-Allow-Headers: Content-Type    │ │ │
│  │  └──────────────────────────────────────────────────┘ │ │
│  │                                                         │ │
│  │  ┌──────────────────────────────────────────────────┐ │ │
│  │  │  HTTP Router                                     │ │ │
│  │  │                                                   │ │ │
│  │  │  Path Pattern Matching:                          │ │ │
│  │  │  - /<npub>/<identifier>.git/*  → Git Handler     │ │ │
│  │  │  - /*                          → Nostr Handler   │ │ │
│  │  └──────────────────────────────────────────────────┘ │ │
│  │                                                         │ │
│  │  ┌────────────────────┐  ┌─────────────────────────┐  │ │
│  │  │  Git HTTP Handler  │  │  Nostr Relay Handler    │  │ │
│  │  │                    │  │                         │  │ │
│  │  │  ┌──────────────┐  │  │  ┌──────────────────┐  │  │ │
│  │  │  │ git-http-    │  │  │  │ WebSocket Upgrade│  │  │ │
│  │  │  │ backend      │  │  │  │                  │  │  │ │
│  │  │  │              │  │  │  │ ┌──────────────┐ │  │  │ │
│  │  │  │ - info/refs  │  │  │  │ │ NIP-01       │ │  │  │ │
│  │  │  │ - upload-pack│  │  │  │ │ - EVENT      │ │  │  │ │
│  │  │  │ - receive-pack  │  │  │ │ - REQ        │ │  │  │ │
│  │  │  │              │  │  │  │ │ - CLOSE      │ │  │  │ │
│  │  │  │ Authorization:  │  │  │ └──────────────┘ │  │  │ │
│  │  │  │ - Query state│  │  │  │                  │  │  │ │
│  │  │  │ - Validate   │  │  │  │ ┌──────────────┐ │  │  │ │
│  │  │  │ - Accept/    │  │  │  │ │ NIP-11       │ │  │  │ │
│  │  │  │   Reject     │  │  │  │ │ - GRASP fields│ │  │  │ │
│  │  │  └──────────────┘  │  │  │ └──────────────┘ │  │  │ │
│  │  │                    │  │  │                  │  │  │ │
│  │  │  Repository:       │  │  │ ┌──────────────┐ │  │  │ │
│  │  │  {GIT_DATA_PATH}/  │  │  │ │ NIP-34       │ │  │  │ │
│  │  │  {npub}/           │  │  │ │ - Announce   │ │  │  │ │
│  │  │  {identifier}.git  │  │  │ │ - State      │ │  │  │ │
│  │  │                    │  │  │ │ - Validate   │ │  │  │ │
│  │  └────────────────────┘  │  │ └──────────────┘ │  │  │ │
│  │                           │  │                  │  │  │ │
│  │                           │  │  HTTP Root:      │  │  │ │
│  │                           │  │  - Serve HTML    │  │  │ │
│  │                           │  │  - NIP-11 JSON   │  │  │ │
│  │                           │  └──────────────────┘  │  │ │
│  │                           │                         │  │ │
│  └───────────────────────────┴─────────────────────────┘  │ │
│                                                             │
│  ┌───────────────────────────────────────────────────────┐ │
│  │  Storage Layer                                        │ │
│  │                                                        │ │
│  │  ┌──────────────────────┐  ┌──────────────────────┐  │ │
│  │  │ Git Repositories     │  │ Nostr Events DB      │  │ │
│  │  │                      │  │                      │  │ │
│  │  │ {GIT_DATA_PATH}/     │  │ {RELAY_DATA_PATH}/   │  │ │
│  │  │ ├── npub1.../        │  │ - Announcements      │  │ │
│  │  │ │   ├── repo1.git/   │  │ - State events       │  │ │
│  │  │ │   └── repo2.git/   │  │ - Issues/Patches     │  │ │
│  │  │ └── npub2.../        │  │ - Other events       │  │ │
│  │  │     └── repo3.git/   │  │                      │  │ │
│  │  └──────────────────────┘  └──────────────────────┘  │ │
│  └───────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
```

---

## Request Flow Examples

### Example 1: Git Clone

```
Client: git clone http://localhost:8080/npub1abc.../my-repo.git
   ↓
actix-web receives HTTP GET request
   ↓
CORS middleware adds headers
   ↓
Router matches path: /npub1abc.../my-repo.git
   ↓
Git Handler receives request
   ↓
git-http-backend processes:
   - GET /npub1abc.../my-repo.git/info/refs?service=git-upload-pack
   ↓
Response includes:
   - CORS headers
   - Git protocol data
   - Capabilities: allow-reachable-sha1-in-want, allow-tip-sha1-in-want
   ↓
Client receives data and clones repository
```

### Example 2: Git Push

```
Client: git push http://localhost:8080/npub1abc.../my-repo.git main
   ↓
actix-web receives HTTP POST request
   ↓
CORS middleware adds headers
   ↓
Router matches path: /npub1abc.../my-repo.git
   ↓
Git Handler receives request
   ↓
BEFORE spawning git-receive-pack:
   1. Parse ref updates from request body
   2. Query latest state announcement from relay
   3. Validate pusher in maintainer set
   4. Validate ref updates match state
   ↓
If validation passes:
   - Spawn git-receive-pack
   - Stream response back to client
   ↓
If validation fails:
   - Return HTTP 403 Forbidden
   - Include error message
   ↓
Client receives success/failure
```

### Example 3: WebSocket Connection (Nostr)

```
Client: new WebSocket('ws://localhost:8080/')
   ↓
actix-web receives HTTP GET with Upgrade: websocket
   ↓
CORS middleware adds headers
   ↓
Router matches path: /
   ↓
Nostr Handler receives request
   ↓
Upgrade to WebSocket
   ↓
Client sends: ["EVENT", {...}]
   ↓
Nostr Handler processes EVENT
   ↓
If kind 30617 (announcement):
   - Validate clone/relays tags
   - Provision Git repository
   - Store event
   ↓
Response: ["OK", event_id, true, ""]
   ↓
Client receives confirmation
```

### Example 4: NIP-11 Request

```
Client: fetch('http://localhost:8080/', {
  headers: { 'Accept': 'application/nostr+json' }
})
   ↓
actix-web receives HTTP GET with Accept header
   ↓
CORS middleware adds headers
   ↓
Router matches path: /
   ↓
Nostr Handler checks Accept header
   ↓
Returns NIP-11 JSON:
{
  "name": "ngit-grasp instance",
  "description": "Rust GRASP implementation",
  "supported_nips": [1, 11, 34],
  "supported_grasps": ["GRASP-01"],
  "repo_acceptance_criteria": "Must list this service in clone and relays tags",
  "curation": "Basic spam prevention"
}
   ↓
Client receives relay information
```

### Example 5: CORS Preflight (OPTIONS)

```
Browser: OPTIONS http://localhost:8080/
Headers:
  - Origin: https://example.com
  - Access-Control-Request-Method: POST
   ↓
actix-web receives OPTIONS request
   ↓
CORS middleware handles preflight
   ↓
Returns 204 No Content with headers:
  - Access-Control-Allow-Origin: *
  - Access-Control-Allow-Methods: GET, POST
  - Access-Control-Allow-Headers: Content-Type
  - Access-Control-Max-Age: 3600
   ↓
Browser caches preflight response
   ↓
Browser proceeds with actual request
```

---

## Component Responsibilities

### actix-web HTTP Server
- Listen on single port
- Route requests by path
- Handle WebSocket upgrades
- Apply CORS to all requests

### CORS Middleware
- Add headers to ALL responses
- Handle OPTIONS preflight
- Allow any origin (GRASP-01 requirement)

### HTTP Router
- Match `/npub.../repo.git` → Git Handler
- Match `/` → Nostr Handler
- Pass through to appropriate handler

### Git Handler
- Serve Git Smart HTTP protocol
- Read from `{GIT_DATA_PATH}/{npub}/{id}.git`
- Validate pushes before accepting
- Return 404 for missing repos

### Nostr Handler
- Upgrade HTTP to WebSocket
- Process NIP-01 messages
- Store/query events
- Serve NIP-11 for HTTP requests
- Provision repos from announcements

### Storage Layer
- Git repositories (bare)
- Nostr events (database)
- Separate paths for each

---

## Configuration Flow

```
Environment Variables
   ↓
.env file (optional)
   ↓
Config::from_env()
   ↓
Config struct:
   - bind_address: "127.0.0.1:8080"
   - domain: "example.com"
   - git_data_path: "./data/repos"
   - relay_data_path: "./data/relay"
   - relay_name: "..."
   - relay_description: "..."
   - owner_npub: "..."
   ↓
Passed to:
   - HTTP server (bind address)
   - Git handler (git_data_path, domain)
   - Nostr handler (relay_data_path, domain, NIP-11 info)
   - Storage layer (both paths)
```

---

## Test Architecture

```
Integration Test
   ↓
TestRelay::start()
   ↓
Spawns ngit-grasp process:
   - NGIT_BIND_ADDRESS=127.0.0.1:{random_port}
   - NGIT_DOMAIN=127.0.0.1:{random_port}
   - NGIT_GIT_DATA_PATH=./test-data/repos
   - NGIT_RELAY_DATA_PATH=./test-data/relay
   ↓
Process starts:
   - actix-web listens on random port
   - Both Git and Nostr available
   ↓
Test runs:
   - Uses grasp-audit library
   - Connects to ws://127.0.0.1:{port}/
   - Runs compliance tests
   ↓
TestRelay::stop()
   ↓
Process killed
   ↓
Test data cleaned up
```

---

## File Structure

```
ngit-grasp/
├── src/
│   ├── main.rs              # Entry point
│   ├── config.rs            # Configuration
│   ├── http/                # NEW - HTTP server
│   │   ├── mod.rs           # Server setup
│   │   ├── git.rs           # Git HTTP handler
│   │   └── nostr.rs         # Nostr WebSocket handler
│   ├── nostr/
│   │   ├── mod.rs
│   │   ├── relay.rs         # Relay logic (reused)
│   │   └── events.rs        # Event handling
│   └── storage/
│       ├── mod.rs
│       └── repository.rs    # Git repo management
├── tests/
│   ├── common/
│   │   ├── mod.rs
│   │   └── relay.rs         # TestRelay fixture
│   ├── nip01_compliance.rs  # NIP-01 tests
│   ├── nip34_announcements.rs  # NIP-34 tests
│   └── grasp01_git_http.rs  # NEW - GRASP-01 Git tests
├── data/                    # Runtime data (gitignored)
│   ├── repos/               # Git repositories
│   └── relay/               # Nostr events
└── test-data/               # Test data (gitignored)
    ├── repos/
    └── relay/
```

---

## Comparison: ngit-relay vs ngit-grasp

### ngit-relay (Go + nginx)

```
nginx (Port 8081)
   ├── Git HTTP → fcgiwrap → git-http-backend
   │                            ↓
   │                     pre-receive hook (Go)
   │                            ↓
   │                     Khatru relay (HTTP API)
   │
   └── Nostr → proxy → Khatru relay (Port 3334)
                            ↓
                     on_event hook (Go)
                            ↓
                     provision repos
```

**Components:**
- nginx (routing)
- fcgiwrap (CGI wrapper)
- git-http-backend (Git protocol)
- pre-receive hook (Go, validates pushes)
- post-receive hook (Go, updates HEAD)
- Khatru relay (Go, Nostr protocol)
- on_event hook (Go, provisions repos)
- supervisord (process management)

### ngit-grasp (Rust)

```
actix-web (Port 8080)
   ├── Git HTTP → git-http-backend crate
   │                   ↓
   │            inline authorization
   │                   ↓
   │            Storage (query state)
   │
   └── Nostr → WebSocket upgrade
                   ↓
              nostr-sdk relay
                   ↓
              on_event (provision repos)
                   ↓
              Storage (store events)
```

**Components:**
- actix-web (routing + HTTP + WebSocket)
- git-http-backend crate (Git protocol)
- nostr-sdk (Nostr protocol)
- Storage (unified storage layer)

**Advantages:**
- Single binary
- No external processes
- Inline authorization (better errors)
- Pure Rust (memory safety)
- Easier testing

---

**Last Updated:** November 4, 2025  
**Purpose:** Reference for implementation