-
Notifications
You must be signed in to change notification settings - Fork 8
/
handle_sitemap.go
54 lines (43 loc) · 1.15 KB
/
handle_sitemap.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
package sitemap
import (
"errors"
"net/http"
"strings"
"github.com/delving/hub3/ikuzo/domain"
"github.com/go-chi/chi"
)
var ErrConfigNotFound = errors.New("sitemap configuration not found")
// handleBaseSitemap is the entry point for all sub-sitemaps.
func (s *Service) handleBaseSitemap(w http.ResponseWriter, r *http.Request) {
cfg, err := s.sitemapConfig(r)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
sm, err := s.sitemapRoot(r.Context(), cfg)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
_, err = sm.WriteTo(w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (s *Service) sitemapConfig(r *http.Request) (Config, error) {
var cfg Config
org, ok := domain.GetOrganization(r)
if !ok {
return cfg, domain.ErrOrgNotFound
}
configID := chi.URLParam(r, configIDKey)
for _, sitemap := range org.Config.Sitemaps {
if strings.EqualFold(sitemap.ID, configID) {
sitemap.OrgID = org.RawID()
return sitemap, nil
}
}
return cfg, ErrConfigNotFound
}