-
Notifications
You must be signed in to change notification settings - Fork 24
/
render_archive.go
70 lines (63 loc) · 1.61 KB
/
render_archive.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 (
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"fiatjaf.com/leafdb"
"github.com/nbd-wtf/go-nostr/nip19"
)
func renderArchive(w http.ResponseWriter, r *http.Request) {
lastIndex := strings.LastIndex(r.URL.Path, "/")
page := 1
if lastIndex != -1 {
pageString := r.URL.Path[lastIndex+1:]
pageInt, err := strconv.Atoi(pageString)
if err != nil {
page = 1
} else {
page = pageInt
}
}
var data []string
pathPrefix := ""
if strings.HasPrefix(r.URL.Path[1:], "npubs-archive") {
pathPrefix = ""
data = make([]string, 0, 5000)
params := leafdb.AnyQuery("pubkey-archive")
params.Skip = (page - 1) * 5000
params.Limit = 5000
for val := range internal.View(params) {
pka := val.(*PubKeyArchive)
npub, _ := nip19.EncodePublicKey(pka.Pubkey)
data = append(data, npub)
}
} else if strings.HasPrefix(r.URL.Path[1:], "relays-archive") {
data = []string{
"pyramid.fiatjaf.com",
"nostr.wine",
"140.f7z.io",
}
}
// Generate a random duration between 2 and 6 hours
minHours := 2
maxHours := 6
randomHours := rand.Intn(maxHours-minHours+1) + minHours
randomDuration := time.Duration(randomHours) * time.Hour
currentTime := time.Now()
modifiedAt := currentTime.Add(-randomDuration).Format("2006-01-02T15:04:05Z07:00")
if len(data) != 0 {
w.Header().Set("Cache-Control", "max-age=3600")
} else {
w.Header().Set("Cache-Control", "max-age=60")
}
w.Header().Add("content-type", "text/xml")
w.Write([]byte(XML_HEADER))
SitemapTemplate.Render(w, &SitemapPage{
Host: s.Domain,
ModifiedAt: modifiedAt,
PathPrefix: pathPrefix,
Data: data,
})
}