-
Notifications
You must be signed in to change notification settings - Fork 14
/
sitemap_controller.go
48 lines (42 loc) · 1.36 KB
/
sitemap_controller.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
package controllers
import (
"github.com/gorilla/mux"
"github.com/larryprice/refermadness/utils"
"gopkg.in/mgo.v2/bson"
"net/http"
)
type SitemapControllerImpl struct {
database utils.DatabaseAccessor
}
func NewSitemapController(database utils.DatabaseAccessor) *SitemapControllerImpl {
return &SitemapControllerImpl{
database: database,
}
}
func (sc *SitemapControllerImpl) Register(router *mux.Router) {
router.HandleFunc("/sitemap.xml", sc.generate)
}
type onlyID struct {
ID bson.ObjectId `bson:"_id"`
}
func (sc *SitemapControllerImpl) generate(w http.ResponseWriter, r *http.Request) {
ids := []onlyID{}
servicesMap := ""
if err := sc.database.Get(r).C("service").Find(nil).All(&ids); err == nil {
for _, id := range ids {
servicesMap += "<url><loc>https://www.refer-madness.com/service/" + id.ID.Hex() +"</loc><changefreq>weekly</changefreq></url>"
}
}
w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>https://www.refer-madness.com/</loc>
</url>
<url>
<loc>https://www.refer-madness.com/legal</loc>
</url>` + servicesMap + "</urlset>"))
}