Server and client stubs.
Implemented HTTP server with configuration getting stub. Implemented CLI client with configuration getting stub.
This commit is contained in:
54
internal/requester/exported.go
Normal file
54
internal/requester/exported.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package requester
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/logger"
|
||||
|
||||
// other
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
var (
|
||||
log zerolog.Logger
|
||||
)
|
||||
|
||||
func Initialize() {
|
||||
log = logger.Logger.With().Str("type", "internal").Str("package", "requester").Logger()
|
||||
log.Info().Msg("Initializing...")
|
||||
}
|
||||
|
||||
func execRequest(method string, url string, data map[string]string) ([]byte, error) {
|
||||
log.Debug().Str("method", method).Str("URL", url).Msg("Trying to execute HTTP request...")
|
||||
|
||||
httpClient := getHTTPClient()
|
||||
|
||||
// Compose HTTP request.
|
||||
// ToDo: POST/PUT/other methods that require body.
|
||||
httpReq, err := http.NewRequest(method, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err1 := httpClient.Do(httpReq)
|
||||
if err1 != nil {
|
||||
return nil, err1
|
||||
}
|
||||
|
||||
bodyBytes, err2 := ioutil.ReadAll(response.Body)
|
||||
if err2 != nil {
|
||||
return nil, err2
|
||||
}
|
||||
response.Body.Close()
|
||||
|
||||
log.Debug().Int("response body length (bytes)", len(bodyBytes)).Msg("Got response")
|
||||
|
||||
return bodyBytes, nil
|
||||
}
|
||||
|
||||
func Get(url string) ([]byte, error) {
|
||||
return execRequest("GET", url, nil)
|
||||
}
|
30
internal/requester/httpclient.go
Normal file
30
internal/requester/httpclient.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package requester
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func getHTTPClient() *http.Client {
|
||||
c := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
// ToDo: configurable.
|
||||
ExpectContinueTimeout: time.Second * 5,
|
||||
DialContext: (&net.Dialer{
|
||||
// ToDo: configurable.
|
||||
Timeout: time.Second * 5,
|
||||
}).DialContext,
|
||||
// ToDo: configurable.
|
||||
ResponseHeaderTimeout: time.Second * 5,
|
||||
// ToDo: configurable.
|
||||
TLSHandshakeTimeout: time.Second * 10,
|
||||
},
|
||||
}
|
||||
|
||||
// ToDo: skip verifying insecure certificates if option was
|
||||
// specified.
|
||||
|
||||
return c
|
||||
}
|
Reference in New Issue
Block a user