commit 1c9604808241076d81ac356411056644f07f2393 Author: Musab Gültekin Date: Thu Jun 6 17:11:19 2019 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1695833 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# IDE directories +.idea/ \ No newline at end of file diff --git a/gezer.go b/gezer.go new file mode 100644 index 0000000..82ba66f --- /dev/null +++ b/gezer.go @@ -0,0 +1,59 @@ +package gezer + +import ( + "io/ioutil" + "net/http" + "time" +) + +type Gezer struct { + client *http.Client + Results chan *Response +} + +type Response struct { + *http.Response + Body []byte +} + +func NewGezer() *Gezer { + return &Gezer{ + client: &http.Client{ + Timeout: time.Second * 10, + }, + Results: make(chan *Response, 1), + } +} + +func (g *Gezer) StartURLs(urls ...string) { + for _, url := range urls { + + // Get request + resp, err := g.client.Get(url) + if err != nil { + if resp != nil { + _ = resp.Body.Close() + } + continue + } + + // Read body + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + continue + } + _ = resp.Body.Close() + + // Create response + response := Response{ + Response: resp, + Body: body, + } + + // Send response + g.Results <- &response + } + + // Close chan, as we finished sending all the results + close(g.Results) +} diff --git a/gezer_test.go b/gezer_test.go new file mode 100644 index 0000000..f578253 --- /dev/null +++ b/gezer_test.go @@ -0,0 +1,14 @@ +package gezer + +import ( + "fmt" + "testing" +) + +func TestGezer_StartURLs(t *testing.T) { + gezer := NewGezer() + gezer.StartURLs("https://api.ipify.org") + for result := range gezer.Results { + fmt.Println(string(result.Body)) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d0b9a6e --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/gogezer/gezer + +go 1.12