-
Notifications
You must be signed in to change notification settings - Fork 15
/
handlers.go
132 lines (117 loc) · 2.8 KB
/
handlers.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
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/fiatjaf/bip340"
"github.com/fiatjaf/relayer"
. "github.com/stevelacy/daz"
)
var head = H("head",
H("meta", Attr{"charset": "utf-8"}),
H("meta", Attr{
"name": "viewport",
"content": "width=device-width, initial-scale=1.0",
}),
H("title", "rsslay"),
)
func handleWebpage(w http.ResponseWriter, r *http.Request) {
items := make([]HTML, 0, 200)
iter := b.db.NewIter(nil)
for iter.First(); iter.Valid(); iter.Next() {
pubkey := string(iter.Key())
var entity Entity
if err := json.Unmarshal(iter.Value(), &entity); err != nil {
continue
}
items = append(items, H("tr",
H("td",
H("code",
pubkey),
),
H("td",
H("a", Attr{
"href": entity.URL}, entity.URL),
),
))
}
body := H("body",
H("h1", "rsslay"),
H("p", "rsslay turns RSS or Atom feeds into ",
H("a", Attr{
"href": "https://github.com/fiatjaf/nostr"}, "Nostr"),
" profiles.",
),
H("h2", "How to use"),
H("ol",
H("li", "Get the blog URL or RSS or Atom feed URL and paste below;"),
H("li", "Click the button to get its corresponding public key"),
H("li", "Add the relay ",
H("code", "wss://"+b.Domain),
" to your Nostr client",
),
H("li", "Follow the feed's public key from your Nostr client."),
),
H("form", Attr{
"action": "/create",
"method": "GET",
"class": "my-4",
},
H("label",
H("input", Attr{
"name": "url",
"type": "url",
"placeholder": "https://.../feed",
}),
),
H("button", "Get Public Key"),
),
H("h2", "Some of the existing feeds"),
H("table", items),
H("h2", "Source Code"),
H("p", "You can find it at ",
H("a", Attr{"href": "https://github.com/fiatjaf/rsslay"},
"https://github.com/fiatjaf/rsslay"),
),
)
w.Header().Set("content-type", "text/html")
w.Write([]byte(
H("html",
head,
body,
)()))
}
func handleCreateFeed(w http.ResponseWriter, r *http.Request) {
url := r.URL.Query().Get("url")
feedurl := getFeedURL(url)
if feedurl == "" {
w.WriteHeader(400)
fmt.Fprint(w, "couldn't find a feed url")
return
}
if _, err := parseFeed(feedurl); err != nil {
w.WriteHeader(400)
fmt.Fprint(w, "bad feed: "+err.Error())
return
}
sk := privateKeyFromFeed(feedurl)
s, err := bip340.ParsePrivateKey(sk)
if err != nil {
w.WriteHeader(500)
fmt.Fprint(w, "bad private key: "+err.Error())
return
}
pubkey := fmt.Sprintf("%x", bip340.GetPublicKey(s))
j, _ := json.Marshal(Entity{
PrivateKey: sk,
URL: feedurl,
})
if err := b.db.Set([]byte(pubkey), j, nil); err != nil {
w.WriteHeader(500)
fmt.Fprint(w, "failure: "+err.Error())
return
}
relayer.Log.Info().Str("url", feedurl).Str("pubkey", pubkey).Msg("saved feed")
fmt.Fprintf(w, "url : %s\npubkey: %s", feedurl, pubkey)
return
}