-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
125 lines (105 loc) · 2.6 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2020-2023 Siemens AG
// This Source Code Form is subject to the terms of
// Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license
// https://creativecommons.org/licenses/by-sa/4.0/
// SPDX-License-Identifier: CC-BY-SA-4.0
package main
import (
"log"
"time"
"github.com/ahmetb/go-linq"
"github.com/go-resty/resty/v2"
"mvdan.cc/xurls/v2"
)
const serviceURL = "http://localhost:8080/checkUrls"
const contentURL = "https://raw.githubusercontent.com/binhnguyennus/awesome-scalability/master/README.md"
func main() {
var text string
timeIt("extracting text", func() {
text = extractText(contentURL)
})
var links []string
timeIt("extracting links", func() {
links = extractLinks(text)
})
count := len(links)
log.Println("Collected", count, "links")
var response CheckURLsResponse
timeIt("checking links", func() {
response = checkURLs(links)
})
groups := linq.From(response.Urls).
Where(func(url interface{}) bool {
urlResponse := url.(URLStatusResponse)
return urlResponse.HTTPStatus >= 300 ||
urlResponse.Status != "ok"
}).
GroupBy(func(url interface{}) interface{} {
return url.(URLStatusResponse).HTTPStatus
}, func(url interface{}) interface{} {
return url
}).
OrderBy(func(group interface{}) interface{} {
return group.(linq.Group).Key
}).
Results()
totalBroken := 0
for _, group := range groups {
g := group.(linq.Group)
for _, lr := range g.Group {
totalBroken++
linkResult := lr.(URLStatusResponse)
log.Printf("Broken link: '%v' %v", linkResult.URL, linkResult.Error)
}
}
log.Printf("Total broken: %v", totalBroken)
}
func checkURLs(links []string) CheckURLsResponse {
request := prepareRequest(links)
var response CheckURLsResponse
_, err := resty.New().R().
SetBody(&request).
SetResult(&response).
Post(serviceURL)
if err != nil {
panic(err)
}
return response
}
func prepareRequest(links []string) CheckURLsRequest {
urls := make([]URLRequest, 0)
for _, link := range links {
if link != "" {
urls = append(urls, URLRequest{
URL: link,
Context: "",
})
} else {
log.Println("link empty: ", link)
}
}
return CheckURLsRequest{
Urls: urls,
}
}
func extractLinks(text string) []string {
rxStrict := xurls.Strict()
links := rxStrict.FindAllString(text, -1)
return links
}
func extractText(url string) string {
client := resty.New()
res, err := client.R().
Get(url)
if err != nil {
panic(err)
}
text := string(res.Body())
return text
}
func timeIt(context string, what func()) {
start := time.Now()
what()
elapsed := time.Since(start)
log.Printf("%v took %s", context, elapsed)
}