-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatuses.go
69 lines (57 loc) · 1.58 KB
/
statuses.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
// active HTTP checks for listed items
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"net/http"
"net/url"
// "time"
)
func checkUrlStatus(url string) (int, error) {
//client := http.Client{
// Timeout: 3 * time.Second,
//}
log.Debug("Checking URL ", url, " for status.")
for {
resp, err := httpClient.Get(url)
if err != nil {
return -1, err
}
defer resp.Body.Close()
if resp.StatusCode == 200 || resp.StatusCode == 401 {
return resp.StatusCode, nil
}
if resp.StatusCode < 300 || resp.StatusCode >= 400 {
return resp.StatusCode, fmt.Errorf("failed to retrieve the content. Status code: %d", resp.StatusCode)
}
url = resp.Header.Get("Location")
if url == "" {
return -1, fmt.Errorf("redirect location not found")
}
}
}
func statusCheckHandler(w http.ResponseWriter, r *http.Request) {
// Extract the URL from the request path
extractedUrl := ""
queryParams, err := url.ParseQuery(r.URL.RawQuery)
for key, values := range queryParams {
for _, value := range values {
if key == "url" {
extractedUrl = value
}
}
}
if extractedUrl == "" {
fmt.Fprintf(w, "%d", 0)
}
// Make an HTTP request to the specified URL
statusCode, err := checkUrlStatus(extractedUrl)
if err != nil {
log.Warn("Request to ", extractedUrl, ", ended with status: ", err)
http.Error(w, fmt.Sprintf("Error making request to %s: %s", extractedUrl, err), http.StatusInternalServerError)
return
}
// Return the status code as the response
log.Debug("Request to ", extractedUrl, ", ended with status: ", statusCode)
fmt.Fprintf(w, "%d", statusCode)
}