From 4d7e8f55088efb7b15ee951ce902e5dc70238e9e Mon Sep 17 00:00:00 2001 From: nxshock Date: Tue, 21 Jun 2022 22:15:09 +0500 Subject: [PATCH] WIP: Add job HTTP-callbacks --- httpclient.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 httpclient.go diff --git a/httpclient.go b/httpclient.go new file mode 100644 index 0000000..9bec559 --- /dev/null +++ b/httpclient.go @@ -0,0 +1,67 @@ +package main + +import ( + "crypto/tls" + "net/http" + "net/url" + "strings" + + log "github.com/sirupsen/logrus" +) + +func createHttpClient() (*http.Client, error) { + httpClient := new(http.Client) + + if config.HttpProxyAddr != "" { + proxyURL, err := url.Parse(config.HttpProxyAddr) + if err != nil { + return nil, err + } + + httpClient.Transport = &http.Transport{ + Proxy: http.ProxyURL(proxyURL), + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} + } + + return httpClient, nil +} + +func httpPost(addr string, text string) error { + httpClient, err := createHttpClient() + if err != nil { + return err + } + + resp, err := httpClient.Post(addr, "text/plain", strings.NewReader(text)) + defer resp.Body.Close() // TODO: нужно ли закрывать Body при наличии ошибки? + if err != nil { + return err + } + + return nil // TODO: вернуть ошибку Post при наличии +} + +func httpGet(addrFmt, jobName, text string) error { + httpClient, err := createHttpClient() + if err != nil { + return err + } + + v := struct { + JobName string + Error string + }{ + JobName: url.QueryEscape(jobName), + Error: url.QueryEscape(text)} + + urlStr := format(addrFmt, v) + log.Println(urlStr) + + resp, err := httpClient.Get(urlStr) + if err != nil { + return err + } + defer resp.Body.Close() // TODO: нужно ли закрывать Body при наличии ошибки? + + return nil +}