35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
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})
|
|
}
|