mirror of
https://github.com/nxshock/gron.git
synced 2024-11-27 03:41:00 +05:00
WIP: Add job HTTP-callbacks
This commit is contained in:
parent
b066901511
commit
4d7e8f5508
67
httpclient.go
Normal file
67
httpclient.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user