Skip to content

Commit b0f7278

Browse files
committed
Add Notification feature which will be executed after a Successful
upload was done.
1 parent aa4681e commit b0f7278

File tree

1 file changed

+104
-6
lines changed

1 file changed

+104
-6
lines changed

notify.go

Lines changed: 104 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,112 @@
11
package upload
22

3-
// TODO: Implent https notification
4-
func (u Upload) SendNotify() error {
3+
import (
4+
"crypto/x509"
5+
"io/ioutil"
6+
"net/http"
7+
"net/url"
8+
"time"
59

6-
/* netClient := &http.Client{
10+
"github.com/pkg/errors"
11+
"go.uber.org/zap"
12+
)
13+
14+
func (u Upload) SendNotify(requuid string) error {
15+
16+
url, urlError := url.Parse(u.NotifyURL)
17+
18+
if urlError != nil {
19+
u.logger.Error("Read caCert error",
20+
zap.String("requuid", requuid),
21+
zap.Error(urlError),
22+
)
23+
return errors.Wrapf(urlError, "URL Parsing error")
24+
}
25+
26+
// https://www.loginradius.com/blog/engineering/http-security-headers/
27+
// https://www.loginradius.com/blog/engineering/tune-the-go-http-client-for-high-performance/
28+
t := http.DefaultTransport.(*http.Transport).Clone()
29+
t.MaxIdleConns = 50
30+
t.MaxConnsPerHost = 50
31+
t.MaxIdleConnsPerHost = 50
32+
t.DisableKeepAlives = true
33+
t.IdleConnTimeout = 30 * time.Second
34+
35+
if u.MyTlsSetting.InsecureSkipVerify {
36+
if url.Scheme != "https" {
37+
u.logger.Error("check Schema insecure",
38+
zap.String("requuid", requuid),
39+
zap.Bool("insecure", u.MyTlsSetting.InsecureSkipVerify),
40+
)
41+
return errors.New("Parameter 'insecure' makes no sense without Scheme https")
42+
}
43+
t.TLSClientConfig.InsecureSkipVerify = true
44+
}
45+
46+
if u.MyTlsSetting.CAPath != "" {
47+
48+
if url.Scheme != "https" {
49+
u.logger.Error("check Schema capath",
50+
zap.String("requuid", requuid),
51+
zap.String("capath", u.MyTlsSetting.CAPath),
52+
)
53+
return errors.New("Parameter 'capath' makes no sense without Scheme https")
54+
}
55+
56+
caCert, err := ioutil.ReadFile(u.MyTlsSetting.CAPath)
57+
if err != nil {
58+
u.logger.Error("Read caCert error",
59+
zap.String("requuid", requuid),
60+
zap.Any("capath", u.MyTlsSetting.CAPath),
61+
zap.Error(err),
62+
)
63+
return errors.Wrapf(err, "failed to read capath %q", u.MyTlsSetting.CAPath)
64+
}
65+
caCertPool := x509.NewCertPool()
66+
successful := caCertPool.AppendCertsFromPEM(caCert)
67+
if !successful {
68+
u.logger.Error("caCertPool.AppendCertsFromPEM error",
69+
zap.String("requuid", requuid),
70+
)
71+
return errors.New("failed to parse ca certificate as PEM encoded content")
72+
}
73+
t.TLSClientConfig.RootCAs = caCertPool
74+
}
75+
76+
httpClient := &http.Client{
777
Timeout: 5 * time.Second,
8-
Transport: http.DefaultTransport,
78+
Transport: t,
79+
}
80+
81+
// TODO: Handle notify Body
82+
myRequest, reqerror := http.NewRequestWithContext(u.ctx, u.NotifyMethod, url.String(), nil)
83+
84+
if reqerror != nil {
85+
u.logger.Error("httpClient build Request error",
86+
zap.String("requuid", requuid),
87+
zap.Any("Request", myRequest),
88+
zap.Error(reqerror),
89+
)
90+
return errors.Wrapf(reqerror, "httpClient build Request error")
991
}
1092

11-
response, _ := netClient.(u.NotifyMethod)(u.NotifyURL)
12-
*/
93+
myRequest.Header.Set("User-Agent", "MyUpload-Handler_v"+Version)
94+
95+
myResp, error := httpClient.Do(myRequest)
96+
if error != nil {
97+
u.logger.Error("httpClient Request error",
98+
zap.String("requuid", requuid),
99+
zap.Any("Request", myRequest),
100+
zap.Any("Response", myResp),
101+
zap.Error(error),
102+
)
103+
return errors.Wrapf(error, "httpClient Request error")
104+
}
105+
106+
u.logger.Debug("Notify Info",
107+
zap.Any("Request", myRequest),
108+
zap.Any("Response", myResp),
109+
)
110+
13111
return nil
14112
}

0 commit comments

Comments
 (0)