generated from Yandex-Practicum/go-musthave-metrics-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate the implementations of grpc and http clients and hide the im…
…plementation with an interface
- Loading branch information
1 parent
b3326fa
commit 18273fa
Showing
7 changed files
with
270 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package base | ||
|
||
import ( | ||
asc "github.com/xoxloviwan/go-monitor/internal/asymcrypto" | ||
) | ||
|
||
type Client struct { | ||
Addr string | ||
Key string | ||
LocalIP string | ||
PublicKey *asc.PublicKey | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package base | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"net" | ||
) | ||
|
||
// GetHash calculates an HMAC-SHA256 hash of the provided data using the given secret key. | ||
// The resulting hash is returned as a hexadecimal-encoded string. | ||
func GetHash(data []byte, strkey string) (string, error) { | ||
h := hmac.New(sha256.New, []byte(strkey)) | ||
_, err := h.Write(data) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
sign := h.Sum(nil) | ||
return hex.EncodeToString(sign), nil | ||
} | ||
|
||
// Compress сжимает слайс байт. | ||
func CompressGzip(data []byte) ([]byte, error) { | ||
var b bytes.Buffer | ||
// создаём переменную w — в неё будут записываться входящие данные, | ||
// которые будут сжиматься и сохраняться в bytes.Buffer | ||
w := gzip.NewWriter(&b) | ||
// запись данных | ||
_, err := w.Write(data) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed write data to compress temporary buffer: %v", err) | ||
} | ||
// обязательно нужно вызвать метод Close() — в противном случае часть данных | ||
// может не записаться в буфер b; если нужно выгрузить все упакованные данные | ||
// в какой-то момент сжатия, используйте метод Flush() | ||
err = w.Close() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed compress data: %v", err) | ||
} | ||
// переменная b содержит сжатые данные | ||
return b.Bytes(), nil | ||
} | ||
|
||
// GetIP returns the local IP address of the machine. | ||
func GetIP() (net.IP, error) { | ||
conn, err := net.Dial("udp", "8.8.8.8:80") | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
defer conn.Close() | ||
|
||
localAddr := conn.LocalAddr().(*net.UDPAddr) | ||
|
||
return localAddr.IP, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package clients | ||
|
||
import ( | ||
asc "github.com/xoxloviwan/go-monitor/internal/asymcrypto" | ||
"github.com/xoxloviwan/go-monitor/internal/clients/grpc" | ||
"github.com/xoxloviwan/go-monitor/internal/clients/http" | ||
api "github.com/xoxloviwan/go-monitor/internal/metrics_types" | ||
) | ||
|
||
type Sender interface { | ||
Send(worker int, msgs api.MetricsList) error | ||
} | ||
|
||
func NewSender(grpcFlag bool, addr string, key string, localIP string, publicKey *asc.PublicKey) Sender { | ||
if grpcFlag { | ||
return &grpc.Client{ | ||
Addr: addr, | ||
Key: key, | ||
LocalIP: localIP, | ||
PublicKey: publicKey, | ||
} // не знаю как избежать здесь дублирования при инициализации разных структур с одинаковым набором полей | ||
} | ||
return &http.Client{ | ||
Addr: addr, | ||
Key: key, | ||
LocalIP: localIP, | ||
PublicKey: publicKey, | ||
} | ||
} |
Oops, something went wrong.