-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap.go
73 lines (58 loc) · 1.51 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package uos
import (
"fmt"
"net/http"
)
type sitemapInfo struct {
allowed []string
disallowed []string
}
func (si *sitemapInfo) allow(route string) {
si.allowed = append(si.allowed, route)
}
func (si *sitemapInfo) disallow(route string) {
si.disallowed = append(si.disallowed, route)
}
func (si *sitemapInfo) addToSitemap(route string, allow bool) {
if allow {
si.allow(route)
} else {
si.disallow(route)
}
}
var sitemap = sitemapInfo{
allowed: []string{},
disallowed: []string{},
}
func setupSitemapHandler() {
Log.DebugContext(
"register robots/sitemap handler",
LogContext{"allowed": len(sitemap.allowed), "disallowed": len(sitemap.disallowed)},
)
appMux.HandleFunc("/robots.txt", robotsHandler)
appMux.HandleFunc("/sitemap.txt", sitemapHandler)
}
func robotsHandler(w http.ResponseWriter, r *http.Request) {
Log.Debug("robots.txt requested")
if r.Method != http.MethodGet {
RespondNotFound(w)
return
}
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "User-agent: *\n")
fmt.Fprintf(w, "Sitemap: %s/sitemap.txt\n\n", Config.Pages["_default"].URL)
for _, url := range sitemap.disallowed {
fmt.Fprintf(w, "Disallow: %s%s\n", Config.Pages["_default"].URL, url)
}
}
func sitemapHandler(w http.ResponseWriter, r *http.Request) {
Log.Debug("sitemap.txt requested")
if r.Method != http.MethodGet {
RespondNotFound(w)
return
}
w.Header().Set("Content-Type", "text/plain")
for _, url := range sitemap.allowed {
fmt.Fprintf(w, "%s%s\n", Config.Pages["_default"].URL, url)
}
}