feeling good
This commit is contained in:
commit
4be8641ffe
3 files changed
+139
No files matched your search
@@ -0,0 +1,17 @@
|
||||
[
|
||||
"https://golang.org",
|
||||
"https://github.com",
|
||||
"https://www.wikipedia.org",
|
||||
"https://example.com",
|
||||
"https://httpbin.org/status/200",
|
||||
"https://httpbin.org/status/201",
|
||||
"https://httpbin.org/status/404",
|
||||
"https://httpbin.org/status/500",
|
||||
"https://httpbin.org/delay/1",
|
||||
"https://pkg.go.dev",
|
||||
"https://neovim.io",
|
||||
"https://archlinux.org",
|
||||
"https://www.kernel.org",
|
||||
"https://sqlite.org",
|
||||
"https://redis.io"
|
||||
]
|
||||
@@ -0,0 +1,119 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Site struct {
|
||||
URL string
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
URL string
|
||||
Status int32
|
||||
Duration time.Duration
|
||||
Error error
|
||||
}
|
||||
|
||||
func ParseSites(rawURLS []string) ([]Site, error) {
|
||||
|
||||
sites := []Site{}
|
||||
http, https := "http://", "https://"
|
||||
for i := range rawURLS {
|
||||
res := strings.HasPrefix(rawURLS[i], http) || strings.HasPrefix(rawURLS[i], https)
|
||||
if !res {
|
||||
return nil, fmt.Errorf("err: [%s] does not contain http or https", rawURLS[i])
|
||||
} else {
|
||||
var site Site
|
||||
site.URL = rawURLS[i]
|
||||
site.Timeout = 5 * time.Second
|
||||
sites = append(sites, site)
|
||||
}
|
||||
}
|
||||
return sites, nil
|
||||
}
|
||||
|
||||
func CheckSite(site Site, c chan Result) {
|
||||
var res Result
|
||||
start := time.Now()
|
||||
resp, err := http.Get(site.URL)
|
||||
end := time.Since(start)
|
||||
if err != nil {
|
||||
res.Error = err
|
||||
res.URL = site.URL
|
||||
res.Duration = end
|
||||
c <- res
|
||||
// return res
|
||||
} else {
|
||||
res.URL = site.URL
|
||||
res.Status = int32(resp.StatusCode)
|
||||
res.Error = nil
|
||||
res.Duration = end
|
||||
|
||||
c <- res
|
||||
}
|
||||
}
|
||||
|
||||
func Report(x []Result) {
|
||||
|
||||
var sucessful, failure uint8
|
||||
for i := range x {
|
||||
if 200 <= x[i].Status && x[i].Status < 300 {
|
||||
sucessful++
|
||||
} else {
|
||||
failure++
|
||||
}
|
||||
}
|
||||
|
||||
var average float64
|
||||
for i := range x {
|
||||
average += x[i].Duration.Seconds()
|
||||
}
|
||||
average = average / float64(len(x))
|
||||
|
||||
fmt.Println("~ Async parsing http and https addresses report ~")
|
||||
fmt.Println()
|
||||
fmt.Printf("Total sites checked: %d\n", len(x))
|
||||
fmt.Printf("Succeeded: %d\n", sucessful)
|
||||
fmt.Printf("Failed: %d\n", failure)
|
||||
fmt.Printf("Average Duration: %f\n", average)
|
||||
fmt.Println()
|
||||
for i := range x {
|
||||
fmt.Printf("Parsed (%d)[%s] in: %f seconds\n", x[i].Status, x[i].URL, x[i].Duration.Seconds())
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
fmt.Println("~ Starting parser ~")
|
||||
result, err := ParseSites(args)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
c := make(chan Result, len(result))
|
||||
var wg sync.WaitGroup
|
||||
x := []Result{}
|
||||
for i := range result {
|
||||
wg.Go(func() {
|
||||
CheckSite(result[i], c)
|
||||
})
|
||||
}
|
||||
for range result {
|
||||
res := <-c
|
||||
x = append(x, res)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
Report(x)
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println("~ Parser finished ~")
|
||||
}
|
||||
Reference in new issue
Block a user