Initial commit
This commit is contained in:
commit
1c96048082
18
.gitignore
vendored
Normal file
18
.gitignore
vendored
Normal 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
59
gezer.go
Normal 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
14
gezer_test.go
Normal 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))
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user