Initial commit

This commit is contained in:
Musab Gültekin 2019-06-06 17:11:19 +03:00
commit 1c96048082
4 changed files with 94 additions and 0 deletions

18
.gitignore vendored Normal file
View File

@ -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/

59
gezer.go Normal file
View File

@ -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)
}

14
gezer_test.go Normal file
View File

@ -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))
}
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/gogezer/gezer
go 1.12