From a269fd32cb6e4c4bb4cf81880c12a30bdf0a27f5 Mon Sep 17 00:00:00 2001 From: Daniele Tonon Date: Thu, 7 Sep 2023 00:36:57 +0200 Subject: [PATCH] Add sitemaps for archives --- main.go | 6 +++++- render_archive.go | 28 ++++++++++++++++++++++++++-- templates/archive.html | 2 +- templates/sitemap.xml | 25 +++++++++++++++++++++++-- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 868bac9..c3b637e 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ var ( s Settings tmpl *template.Template templateMapping = make(map[string]string) - log = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger() + log = zerolog.New(os.Stderr).Output(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger() ) func updateArchives(ctx context.Context) { @@ -67,6 +67,8 @@ func main() { templateMapping["address"] = "other.html" templateMapping["relay"] = "relay.html" templateMapping["relay_sitemap"] = "sitemap.xml" + templateMapping["archive"] = "archive.html" + templateMapping["archive_sitemap"] = "sitemap.xml" funcMap := template.FuncMap{ "basicFormatting": basicFormatting, @@ -90,6 +92,8 @@ func main() { http.Handle("/njump/static/", http.StripPrefix("/njump/", http.FileServer(http.FS(static)))) http.HandleFunc("/npubs-archive/", renderArchive) http.HandleFunc("/relays-archive/", renderArchive) + http.HandleFunc("/npubs-archive.xml", renderArchive) + http.HandleFunc("/relays-archive.xml", renderArchive) http.HandleFunc("/", render) log.Print("listening at http://0.0.0.0:" + s.Port) diff --git a/render_archive.go b/render_archive.go index 455b4a1..e5bf1fd 100644 --- a/render_archive.go +++ b/render_archive.go @@ -2,15 +2,26 @@ package main import ( "fmt" + "math/rand" "net/http" "strconv" "strings" + "time" "github.com/nbd-wtf/go-nostr/nip19" ) func renderArchive(w http.ResponseWriter, r *http.Request) { + code := r.URL.Path[1:] + hostname := code[2:] + typ := "archive" resultsPerPage := 50 + + if strings.HasSuffix(hostname, ".xml") { + typ = "archive_sitemap" + resultsPerPage = 5000 + } + lastIndex := strings.LastIndex(r.URL.Path, "/") page := 1 if lastIndex != -1 { @@ -26,7 +37,11 @@ func renderArchive(w http.ResponseWriter, r *http.Request) { prefix := "" path_prefix := "" title := "" - area := strings.Split(r.URL.Path[1:], "/")[0] + area := "" + if strings.HasPrefix(r.URL.Path[1:], "npubs-archive") { + area = "npubs-archive" + } + if area == "npubs-archive" { prefix = "pa" path_prefix = "" @@ -55,10 +70,19 @@ func renderArchive(w http.ResponseWriter, r *http.Request) { nextPage = 0 } + // 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") + params := map[string]any{ "title": title, "pathPrefix": path_prefix, "data": data, + "modifiedAt": modifiedAt, "paginationUrl": area, "nextPage": fmt.Sprint(nextPage), "prevPage": fmt.Sprint(prevPage), @@ -71,7 +95,7 @@ func renderArchive(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "max-age=60") } - if err := tmpl.ExecuteTemplate(w, "archive.html", params); err != nil { + if err := tmpl.ExecuteTemplate(w, templateMapping[typ], params); err != nil { log.Error().Err(err).Msg("error rendering") return } diff --git a/templates/archive.html b/templates/archive.html index 32abd80..b185e48 100644 --- a/templates/archive.html +++ b/templates/archive.html @@ -31,7 +31,7 @@
- {{range $index, $element := .data }} + {{range $element := .data }}
{{$element | escapeString}}
diff --git a/templates/sitemap.xml b/templates/sitemap.xml index 397e0e1..26884e0 100644 --- a/templates/sitemap.xml +++ b/templates/sitemap.xml @@ -1,12 +1,22 @@ +{{if .npub}} - {{if .npub}}https://{{.CanonicalHost}}/{{.npub | escapeString}}{{end}} - {{if .hostname}}https://{{.CanonicalHost}}/r/{{.hostname | escapeString}}{{end}} + https://{{.CanonicalHost}}/{{.npub | escapeString}} {{.modifiedAt | escapeString}} daily 0.8 +{{end}} +{{if .hostname}} + + https://{{.CanonicalHost}}/r/{{.hostname | escapeString}} + {{.modifiedAt | escapeString}} + daily + 0.8 + +{{end}} +{{if .lastNotes}} {{range .lastNotes}} https://{{$.CanonicalHost}}/{{.Nevent | escapeString}} @@ -15,4 +25,15 @@ 0.5 {{end}} +{{end}} +{{if .data}} +{{range $element := .data }} + + https://{{$.CanonicalHost}}/{{$.pathPrefix}}{{$element | trimProtocol | escapeString}} + {{$.modifiedAt | escapeString}} + daily + 0.5 + +{{end}} +{{end}}