-
Notifications
You must be signed in to change notification settings - Fork 4
/
gocheck.go
81 lines (63 loc) · 1.4 KB
/
gocheck.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
74
75
76
77
78
79
80
81
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"./sitemap"
"./urlstatus"
)
const CheckInterval = 2
var httpAddr = fmt.Sprintf(":%s", getEnv("PORT", "3000"))
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func checkSitemap(urls sitemap.URLs) {
for _, anUrl := range urls.Locs {
time.Sleep(CheckInterval * time.Second)
statusCode, err := getHTTPStatus(anUrl)
if err != nil {
continue
}
urlstatus.SetUrlHTTPStatus(anUrl, statusCode)
fmt.Println(anUrl, statusCode)
}
}
func getHTTPStatus(anUrl string) (int, error) {
resp, err := http.Get(anUrl)
if err != nil {
return 0, err
}
return resp.StatusCode, nil
}
func serveHTTPStatuses(w http.ResponseWriter, r *http.Request) {
js, err := json.Marshal(urlstatus.GetUrlStatuses())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func main() {
siteMapUrls := os.Getenv("SITEMAP")
if siteMapUrls == "" {
log.Fatal("No sitemap url passed!")
os.Exit(1)
}
sitemap.InitSitemap(siteMapUrls)
//Listen to port to serve url statuses
http.HandleFunc("/", serveHTTPStatuses)
log.Println("Starting server ", httpAddr)
go func() {
for {
checkSitemap(sitemap.GetSitemapURLs())
}
}()
log.Println(http.ListenAndServe(httpAddr, nil))
}