Add sitemaps for archives

This commit is contained in:
Daniele Tonon
2023-09-07 00:36:57 +02:00
parent b817bc0e54
commit a269fd32cb
4 changed files with 55 additions and 6 deletions

View File

@@ -67,6 +67,8 @@ func main() {
templateMapping["address"] = "other.html" templateMapping["address"] = "other.html"
templateMapping["relay"] = "relay.html" templateMapping["relay"] = "relay.html"
templateMapping["relay_sitemap"] = "sitemap.xml" templateMapping["relay_sitemap"] = "sitemap.xml"
templateMapping["archive"] = "archive.html"
templateMapping["archive_sitemap"] = "sitemap.xml"
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"basicFormatting": basicFormatting, "basicFormatting": basicFormatting,
@@ -90,6 +92,8 @@ func main() {
http.Handle("/njump/static/", http.StripPrefix("/njump/", http.FileServer(http.FS(static)))) http.Handle("/njump/static/", http.StripPrefix("/njump/", http.FileServer(http.FS(static))))
http.HandleFunc("/npubs-archive/", renderArchive) http.HandleFunc("/npubs-archive/", renderArchive)
http.HandleFunc("/relays-archive/", renderArchive) http.HandleFunc("/relays-archive/", renderArchive)
http.HandleFunc("/npubs-archive.xml", renderArchive)
http.HandleFunc("/relays-archive.xml", renderArchive)
http.HandleFunc("/", render) http.HandleFunc("/", render)
log.Print("listening at http://0.0.0.0:" + s.Port) log.Print("listening at http://0.0.0.0:" + s.Port)

View File

@@ -2,15 +2,26 @@ package main
import ( import (
"fmt" "fmt"
"math/rand"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/nbd-wtf/go-nostr/nip19" "github.com/nbd-wtf/go-nostr/nip19"
) )
func renderArchive(w http.ResponseWriter, r *http.Request) { func renderArchive(w http.ResponseWriter, r *http.Request) {
code := r.URL.Path[1:]
hostname := code[2:]
typ := "archive"
resultsPerPage := 50 resultsPerPage := 50
if strings.HasSuffix(hostname, ".xml") {
typ = "archive_sitemap"
resultsPerPage = 5000
}
lastIndex := strings.LastIndex(r.URL.Path, "/") lastIndex := strings.LastIndex(r.URL.Path, "/")
page := 1 page := 1
if lastIndex != -1 { if lastIndex != -1 {
@@ -26,7 +37,11 @@ func renderArchive(w http.ResponseWriter, r *http.Request) {
prefix := "" prefix := ""
path_prefix := "" path_prefix := ""
title := "" 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" { if area == "npubs-archive" {
prefix = "pa" prefix = "pa"
path_prefix = "" path_prefix = ""
@@ -55,10 +70,19 @@ func renderArchive(w http.ResponseWriter, r *http.Request) {
nextPage = 0 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{ params := map[string]any{
"title": title, "title": title,
"pathPrefix": path_prefix, "pathPrefix": path_prefix,
"data": data, "data": data,
"modifiedAt": modifiedAt,
"paginationUrl": area, "paginationUrl": area,
"nextPage": fmt.Sprint(nextPage), "nextPage": fmt.Sprint(nextPage),
"prevPage": fmt.Sprint(prevPage), "prevPage": fmt.Sprint(prevPage),
@@ -71,7 +95,7 @@ func renderArchive(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=60") 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") log.Error().Err(err).Msg("error rendering")
return return
} }

View File

@@ -31,7 +31,7 @@
<div class="field separator long"></div> <div class="field separator long"></div>
<div class="field last_notes"> <div class="field last_notes">
{{range $index, $element := .data }} {{range $element := .data }}
<a href="/{{$.pathPrefix}}{{$element | trimProtocol | escapeString}}" class="note"> <a href="/{{$.pathPrefix}}{{$element | trimProtocol | escapeString}}" class="note">
<div class="content">{{$element | escapeString}}</div> <div class="content">{{$element | escapeString}}</div>
</a> </a>

View File

@@ -1,12 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{if .npub}}
<url> <url>
{{if .npub}}<loc>https://{{.CanonicalHost}}/{{.npub | escapeString}}</loc>{{end}} <loc>https://{{.CanonicalHost}}/{{.npub | escapeString}}</loc>
{{if .hostname}}<loc>https://{{.CanonicalHost}}/r/{{.hostname | escapeString}}</loc>{{end}}
<lastmod>{{.modifiedAt | escapeString}}</lastmod> <lastmod>{{.modifiedAt | escapeString}}</lastmod>
<changefreq>daily</changefreq> <changefreq>daily</changefreq>
<priority>0.8</priority> <priority>0.8</priority>
</url> </url>
{{end}}
{{if .hostname}}
<url>
<loc>https://{{.CanonicalHost}}/r/{{.hostname | escapeString}}</loc>
<lastmod>{{.modifiedAt | escapeString}}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
{{end}}
{{if .lastNotes}}
{{range .lastNotes}} {{range .lastNotes}}
<url> <url>
<loc>https://{{$.CanonicalHost}}/{{.Nevent | escapeString}}</loc> <loc>https://{{$.CanonicalHost}}/{{.Nevent | escapeString}}</loc>
@@ -15,4 +25,15 @@
<priority>0.5</priority> <priority>0.5</priority>
</url> </url>
{{end}} {{end}}
{{end}}
{{if .data}}
{{range $element := .data }}
<url>
<loc>https://{{$.CanonicalHost}}/{{$.pathPrefix}}{{$element | trimProtocol | escapeString}}</loc>
<lastmod>{{$.modifiedAt | escapeString}}</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
{{end}}
{{end}}
</urlset> </urlset>