-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
70 lines (60 loc) · 1.76 KB
/
web.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
package main
import (
"encoding/json"
"html/template"
"log"
"net/http"
"net/url"
"github.com/nbd-wtf/go-nostr/nip05"
"github.com/nbd-wtf/go-nostr/nip19"
)
func (a *Atomstr) webMain(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/index.tmpl"))
feeds := a.dbGetAllFeeds()
data := webIndex{
Relays: relaysToPublishTo,
Feeds: *feeds,
}
tmpl.Execute(w, data)
}
func (a *Atomstr) webAdd(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/add.tmpl"))
feedItem, err := a.addSource(r.FormValue("url"))
var status string
if err != nil {
status = "No feed found or feed already exists."
} else {
feedItem.Npub, _ = nip19.EncodePublicKey(feedItem.Pub)
status = "Success! Check your feed below and open it with your preferred app."
}
data := webAddFeed{
Status: status,
Feed: *feedItem,
}
tmpl.Execute(w, data)
}
func (a *Atomstr) webNip05(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
name, _ = url.QueryUnescape(name)
w.Header().Set("Content-Type", "application/json")
var response []byte
if name != "" && name != "_" {
feedItem := a.dbGetFeed(name)
nip05WellKnownResponse := nip05.WellKnownResponse{
Names: map[string]string{
name: feedItem.Pub,
},
Relays: nil,
}
response, _ = json.Marshal(nip05WellKnownResponse)
_, _ = w.Write(response)
}
}
func (a *Atomstr) webserver() {
http.HandleFunc("/", a.webMain)
http.HandleFunc("/add", a.webAdd)
http.HandleFunc("/.well-known/nostr.json", a.webNip05)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
log.Println("[INFO] Starting webserver at port", webserverPort)
log.Fatal(http.ListenAndServe(":"+webserverPort, nil))
}