feat(seo): add sitemap generator

This commit is contained in:
Krispire Admin
2026-06-15 13:28:15 +05:30
parent 438258ade0
commit bbe491ae13
4 changed files with 299 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
package handler
import (
"context"
"net/http"
"time"
"github.com/prabhupadasays/spsays-bff/internal/repository"
"github.com/prabhupadasays/spsays-bff/pkg/response"
)
// SitemapHandler serves the flat list of public content URLs the frontend
// turns into sitemap XML. Public + heavily cached on the frontend (daily
// ISR), so this expensive-ish sweep runs ~once/day, never per crawler request.
type SitemapHandler struct{ repo *repository.SitemapRepo }
func NewSitemapHandler(repo *repository.SitemapRepo) *SitemapHandler {
return &SitemapHandler{repo: repo}
}
// GetContent — GET /sitemap/content → { "urls": ["/books/bg/1/1", …] }.
func (h *SitemapHandler) GetContent(w http.ResponseWriter, r *http.Request) {
// The sweep touches tens of thousands of docs across several collections;
// give it room but bound it so a stuck query can't hang the handler.
ctx, cancel := context.WithTimeout(r.Context(), 90*time.Second)
defer cancel()
urls, err := h.repo.ContentURLs(ctx)
if err != nil {
response.Error(w, http.StatusInternalServerError, "could not build sitemap")
return
}
response.JSON(w, http.StatusOK, map[string]any{"urls": urls})
}