From 4be8641ffe961bd083bd74059075947d21882769 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Wed, 4 Nov 2026 18:28:02 -0500 Subject: [PATCH] feeling good --- addr.json | 17 ++++++++ go.mod | 3 ++ main.go | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 addr.json create mode 100644 go.mod create mode 100644 main.go diff --git a/addr.json b/addr.json new file mode 100644 index 0000000..1b76d3a --- /dev/null +++ b/addr.json @@ -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" +] diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c588313 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module com.DarthMooMancer.learning.go + +go 1.27.3 diff --git a/main.go b/main.go new file mode 100644 index 0000000..dab62dd --- /dev/null +++ b/main.go @@ -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 ~") +}