-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnip05.go
64 lines (50 loc) · 1.1 KB
/
nip05.go
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
package main
import (
"encoding/json"
"log"
"net/http"
"path"
)
type Response struct {
Names map[string]string `json:"names"`
Relays map[string][]string `json:"relays"`
}
func NIP05Handler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
if name == "" {
http.Error(w, "Missing query parameter 'name'", http.StatusBadRequest)
return
}
doc := loadNIP05()
if doc == nil {
http.Error(w, "Something went wrong", http.StatusInternalServerError)
return
}
log.Println(doc)
resp := Response{
Names: make(map[string]string, 1),
Relays: make(map[string][]string, 1),
}
pubKey, ok := doc.Names[name]
if !ok {
http.Error(w, "Can't find this name", http.StatusNotFound)
return
}
resp.Names[name] = pubKey
relays, ok := doc.Relays[pubKey]
if ok {
resp.Relays[pubKey] = relays
}
json.NewEncoder(w).Encode(resp)
}
func loadNIP05() *Response {
resp := new(Response)
data, err := ReadFile(path.Join(config.WorkingDirectory, "/nip05.json"))
if err != nil {
return nil
}
if err := json.Unmarshal(data, resp); err != nil {
return nil
}
return resp
}