forked from ikeikeikeike/go-sitemap-generator
/
ping.go
48 lines (40 loc) · 1.15 KB
/
ping.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 stm
import (
"fmt"
"net/http"
"time"
)
// PingSearchEngines requests some ping server from it calls Sitemap.PingSearchEngines.
func PingSearchEngines(opts *Options, urls ...string) {
urls = append(urls, []string{
"http://www.google.com/webmasters/tools/ping?sitemap=%s",
"http://www.bing.com/webmaster/ping.aspx?siteMap=%s",
}...)
indexLocation := opts.IndexLocation()
sitemapURLs := []string{indexLocation.URL()}
for _, alternativeURL := range indexLocation.AlternativeURLs() {
sitemapURLs = append(sitemapURLs, alternativeURL)
}
bufs := len(urls)
does := make(chan string, bufs)
client := http.Client{Timeout: time.Duration(5 * time.Second)}
for _, url := range urls {
for _, sitemapURL := range sitemapURLs {
go func(baseurl, sitemapURL string) {
url := fmt.Sprintf(baseurl, sitemapURL)
println("Ping now:", url)
resp, err := client.Get(url)
if err != nil {
does <- fmt.Sprintf("[E] Ping failed: %s (URL:%s)",
err, url)
return
}
defer resp.Body.Close()
does <- fmt.Sprintf("Successful ping of `%s`", url)
}(url, sitemapURL)
}
}
for i := 0; i < bufs; i++ {
println(<-does)
}
}