generated from dashpay/go-engineer-code-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
146 lines (117 loc) · 2.83 KB
/
service.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package usecase
import (
"bytes"
"compress/gzip"
"context"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/json"
"io"
"net/http"
"os"
"github.com/EugeneTseitlin/dash-go-code-challenge/internal/app/validation"
)
// Service is a service which should interact with p2p and / or self-hosted services in a network
type Service struct {
p2pClient *http.Client
selfHostedClient *http.Client
}
// NewService returns a new service
func NewService(p2pClient, selfHostedClient *http.Client) *Service {
return &Service{
p2pClient: p2pClient,
selfHostedClient: selfHostedClient,
}
}
// Fetch returns a fetched data
func (s *Service) Fetch(ctx context.Context) ([]map[string]interface{}, error) {
p2pURL := os.Getenv("P2P_URL")
resp, err := s.p2pClient.Get(p2pURL + "/data")
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
decryptionResult, err := decrypt(body)
if err != nil {
return nil, err
}
var decompressionResult bytes.Buffer
r, err := gzip.NewReader(bytes.NewReader(decryptionResult))
if err != nil {
return nil, err
}
io.Copy(&decompressionResult, r)
r.Close()
var result []map[string]interface{}
err = json.Unmarshal(decompressionResult.Bytes(), &result)
if err != nil {
return nil, err
}
return result, nil
}
// Store stored a passed items in external service(s)
func (s *Service) Store(ctx context.Context, items []map[string]interface{}) error {
var err error
v := validation.CreateValidator()
err = validation.ValidateData(v, items)
if err != nil {
return err
}
body, err := json.Marshal(items)
if err != nil {
return err
}
var compressionResult bytes.Buffer
w := gzip.NewWriter(&compressionResult)
w.Write(body)
w.Close()
encryptionResult, err := encrypt(compressionResult.Bytes())
if err != nil {
return err
}
p2pURL := os.Getenv("P2P_URL")
_, err = s.p2pClient.Post(p2pURL + "/data", "application/json", bytes.NewReader(encryptionResult))
if err != nil {
return err
}
return nil
}
func encrypt(input []byte) ([]byte, error) {
c, err := aes.NewCipher([]byte(os.Getenv("AES_SECRET")))
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
_, err = io.ReadFull(rand.Reader, nonce)
if err != nil {
return nil, err
}
output := gcm.Seal(nonce, nonce, input, nil)
return output, nil
}
func decrypt(input []byte) ([]byte, error) {
c, err := aes.NewCipher([]byte(os.Getenv("AES_SECRET")))
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
nonce, ciphertext := input[:nonceSize], input[nonceSize:]
output, err := gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return output, nil
}