Skip to content

Commit 370862e

Browse files
authored
Create DISCOVERY.md
1 parent 8ce6b34 commit 370862e

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

docs/DISCOVERY.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## Discovery API
2+
3+
- [x] POST `/subscribe/{secretHash}` - Register a peer
4+
- [x] GET `/discovery/{secretHash}` - Get list of active peers
5+
- [x] DELETE `/unsubscribe/{secretHash}/{peerId}` - Remove peer
6+
7+
### Start Server
8+
```sql
9+
D SELECT httpserve_enable_discovery('true');
10+
D SELECT httpserve_start('0.0.0.0',9999, '');
11+
┌──────────────────────────────────────┐
12+
│ httpserve_start('0.0.0.0', 9999, '') │
13+
varchar
14+
├──────────────────────────────────────┤
15+
│ HTTP server started on 0.0.0.0:9999
16+
└──────────────────────────────────────┘
17+
```
18+
19+
### Register a Peer under a secret hash
20+
#### CURL
21+
```bash
22+
curl -X POST "https://localhost:9999/subscribe/secretHash" \
23+
-H "Content-Type: application/json" \
24+
-d '{ "name": "service1", "endpoint": "http://192.168.1.100:8080", "ttl": 300 }
25+
```
26+
#### SQL
27+
```sql
28+
INSTALL http_client FROM community; LOAD http_client; LOAD json;
29+
WITH __input AS (
30+
SELECT
31+
http_post(
32+
'http://localhost:9999/subscribe/secretHash',
33+
headers => MAP {
34+
},
35+
params => MAP {
36+
'name': 'quackpipe1',
37+
'endpoint': 'https://1.1.1.1',
38+
}
39+
) AS res
40+
) SELECT res->>'reason' as res, res->>'status' as status FROM __input;
41+
```
42+
43+
### Check `peers` table
44+
```sql
45+
D SELECT name, endpoint, source_address as sourceAddress, peer_id as peerId, metadata, ttl, strftime(registered_at, '%Y-%m-%d %H:%M:%S') as registered_at FROM peers WHERE hash = 'secretHash';
46+
┌──────────┬───────────────────────────┬───────────────┬──────────────────────────────────┬──────────┬───────┬─────────────────────┐
47+
│ name │ endpoint │ sourceAddress │ peerId │ metadata │ ttl │ registered_at │
48+
│ varchar │ varchar │ varchar │ varchar │ varchar │ int64 │ varchar │
49+
├──────────┼───────────────────────────┼───────────────┼──────────────────────────────────┼──────────┼───────┼─────────────────────┤
50+
│ service1 │ http://192.168.1.100:8080 │ xxx.xx.xx.xxx │ 0872c98634ce7e608e19aa1a1e6cf784 │ {} │ 300 │ 2024-11-14 19:44:23 │
51+
└──────────┴───────────────────────────┴───────────────┴──────────────────────────────────┴──────────┴───────┴─────────────────────┘
52+
```
53+
54+
### Discover Peers
55+
#### CURL
56+
```bash
57+
curl "http://localhost:9999/discovery/secretHash"
58+
```
59+
#### SQL
60+
```sql
61+
D SELECT * FROM read_ndjson_auto('http://localhost:9999/discovery/secretHash');
62+
┌──────────┬──────────────────────┬────────────────┬──────────────────────────────┬──────────┬─────────┬─────────────────────┐
63+
│ name │ endpoint │ source_address │ peer_id │ metadata │ ttl │ registered_at │
64+
│ varchar │ varchar │ varchar │ uuid │ varchar │ varchar │ timestamp │
65+
├──────────┼──────────────────────┼────────────────┼──────────────────────────────┼──────────┼─────────┼─────────────────────┤
66+
│ service1 │ http://192.168.1.1… │ 127.0.0.1 │ 0872c986-34ce-7e60-8e19-aa… │ │ 3600 │ 2024-11-15 14:13:50 │
67+
└──────────┴──────────────────────┴────────────────┴──────────────────────────────┴──────────┴─────────┴─────────────────────┘
68+
D
69+
```
70+
71+
⚠️ minor issue with peer_id being a UUID and clients hating it

0 commit comments

Comments
 (0)