-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap.go
73 lines (60 loc) · 1.56 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 main
import (
"fmt"
"net/http"
"os"
"path"
"time"
"github.com/snabb/sitemap"
)
// GenSiteMap generate sitemap.
func GenSiteMap(posts Posts, outputPath string) {
sm := sitemap.New()
t := time.Now().UTC()
for _, v := range posts {
sm.Add(&sitemap.URL{
Loc: config.Host + "/posts/" + v.MetaData.Permanent + ".html",
LastMod: &t,
ChangeFreq: sitemap.Weekly,
})
}
//index.html
sm.Add(&sitemap.URL{
Loc: config.Host + "/",
LastMod: &t,
ChangeFreq: sitemap.Weekly,
})
pathString := path.Join(outputPath, "/posts/sitemap.xml")
f, err := os.Create(pathString)
checkError(err)
sm.WriteTo(f)
PingSearchEngines()
}
// PingSearchEngines requests some ping server from it calls Sitemap.PingSearchEngines.
// Modify from: github.com/ikeikeikeike/go-sitemap-generator/stm"
func PingSearchEngines(urls ...string) {
urls = append(urls, []string{
"http://www.google.com/webmasters/tools/ping?sitemap=%s",
"http://www.bing.com/webmaster/ping.aspx?siteMap=%s",
}...)
bufs := len(urls)
does := make(chan string, bufs)
client := http.Client{Timeout: time.Duration(5 * time.Second)}
for _, url := range urls {
go func(baseurl string) {
pingURL := fmt.Sprintf(baseurl, config.Host+"/sitemap.xml")
println("Ping now:", pingURL)
resp, err := client.Get(pingURL)
if err != nil {
does <- fmt.Sprintf("[E] Ping failed: %s (URL:%s)",
err, pingURL)
return
}
defer resp.Body.Close()
does <- fmt.Sprintf("Successful ping of `%s`", pingURL)
}(url)
}
for i := 0; i < bufs; i++ {
println(<-does)
}
}