-
Notifications
You must be signed in to change notification settings - Fork 0
/
sources.go
97 lines (82 loc) · 2.06 KB
/
sources.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package cfggo
import (
"bytes"
"io"
"net/http"
"os"
)
type configHandler interface {
LoadConfig() ([]byte, error)
SaveConfig(data []byte) error
}
type handlerFile struct {
filename string
}
func (h *handlerFile) LoadConfig() ([]byte, error) {
if h.filename == "" {
return nil, nil
}
data, err := os.ReadFile(h.filename)
if err != nil {
return nil, ErrorWrapper(err, 0, "")
}
return data, nil
}
func (h *handlerFile) SaveConfig(data []byte) error {
if h.filename == "" {
return ErrorWrapper(nil, 400, "filename is empty")
}
err := os.WriteFile(h.filename, data, 0644)
if err != nil {
return ErrorWrapper(err, 0, "")
}
return nil
}
type handlerHTTP struct {
source http.Request
dest http.Request
}
func (h *handlerHTTP) LoadConfig() ([]byte, error) {
if h.source.URL.String() == "" {
return nil, ErrorWrapper(nil, 400, "source URL is empty")
}
req, err := http.NewRequest(h.source.Method, h.source.URL.String(), h.source.Body)
if err != nil {
return nil, ErrorWrapper(err, 0, "")
}
req.Header = h.source.Header
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, ErrorWrapper(err, 0, "")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, ErrorWrapper(nil, resp.StatusCode, "failed to load config from HTTP source")
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, ErrorWrapper(err, 0, "")
}
return data, nil
}
func (h *handlerHTTP) SaveConfig(data []byte) error {
if h.dest.URL.String() == "" {
return ErrorWrapper(nil, 400, "destination URL is empty")
}
req, err := http.NewRequest(h.dest.Method, h.dest.URL.String(), bytes.NewReader(data))
if err != nil {
return ErrorWrapper(err, 0, "")
}
req.ContentLength = int64(len(data))
req.Header = h.dest.Header
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return ErrorWrapper(err, 0, "")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return ErrorWrapper(nil, resp.StatusCode, "failed to save config to HTTP destination")
}
return nil
}