Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func handlers(t *testing.T) http.Handler {

func TestAgent(t *testing.T) {
settings := mockSettings(t)
listener, err := net.Listen("tcp", settings.Address())
listener, err := net.Listen("tcp", settings.Server.Address.String())
assert.NoError(t, err)

server := httptest.NewUnstartedServer(handlers(t))
Expand All @@ -72,7 +72,7 @@ func TestAgent(t *testing.T) {
defer server.Close()

var wg sync.WaitGroup
client := app.NewHTTPClient(settings)
client := app.NewClient(settings)
wg.Add(3)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
go client.CollectAdditionalMetrics(ctx, &wg)
Expand Down
2 changes: 1 addition & 1 deletion cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {

settings := config.NewConfig()
var wg sync.WaitGroup
client := app.NewHTTPClient(settings)
client := app.NewClient(settings)
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)

wg.Add(3)
Expand Down
3 changes: 2 additions & 1 deletion cmd/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/stretchr/testify/suite"

"github.com/syols/go-devops/config"
"github.com/syols/go-devops/internal/app"
"github.com/syols/go-devops/internal/models"
Expand Down Expand Up @@ -170,7 +171,7 @@ func (suite *MetricSuite) TestUpdateCounterJSON() {
func (suite *MetricSuite) check(mock Mock) {
uri := url.URL{
Scheme: "http",
Host: suite.settings.Address(),
Host: suite.settings.Server.Address.String(),
Path: mock.route,
}

Expand Down
61 changes: 51 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"strconv"
"time"
Expand All @@ -19,12 +20,14 @@ type Config struct {
Server ServerConfig `yaml:"server" json:"server"`
Agent AgentConfig `yaml:"agent" json:"agent"`
Store StoreConfig `yaml:"store" json:"store"`
Grpc *GrpcConfig `yaml:"grpc,omitempty" json:"grpc,omitempty"`
}

// ServerConfig Server config struct
type ServerConfig struct {
Address Address `yaml:"address" json:"address"`
Key *string `yaml:"key,omitempty" json:"key,omitempty"`
Address Address `yaml:"address" json:"address"`
Key *string `yaml:"key,omitempty" json:"key,omitempty"`
TrustedSubnet *string `yaml:"trusted_subnet,omitempty" json:"trusted_subnet,omitempty"`
}

// AgentConfig Agent config struct
Expand All @@ -43,6 +46,11 @@ type StoreConfig struct {
StoreInterval time.Duration `yaml:"store_interval" json:"store_interval"`
}

// GrpcConfig Store Grpc config
type GrpcConfig struct {
Address Address `yaml:"address" json:"address"`
}

// Address struct
type Address struct {
Host string `yaml:"host"`
Expand All @@ -65,8 +73,8 @@ func (s *Config) LoadFromEnvironment() {
}

// Address create HTTP address
func (s *Config) Address() string {
return fmt.Sprintf("%s:%d", s.Server.Address.Host, s.Server.Address.Port)
func (a *Address) String() string {
return fmt.Sprintf("%s:%d", a.Host, a.Port)
}

// String create string from config
Expand Down Expand Up @@ -95,17 +103,50 @@ func (s *Config) setDefault(configPath string) error {
return nil
}

func withAddress(address string) Option {
func withHTTPAddress(address string) Option {
return func(s *Config) {
host, port, err := net.SplitHostPort(address)
if err != nil {
log.Fatal(err.Error())
}

value, err := strconv.ParseUint(port, 0, 16)
if err != nil {
log.Fatal(err.Error())
}

s.Server.Address.Host = host
s.Server.Address.Port = uint16(value)
}
}

func withGrpcAddress(address string) Option {
return func(s *Config) {
if host, port, err := net.SplitHostPort(address); err == nil {
if port, err := strconv.ParseUint(port, 0, 16); err == nil {
s.Server.Address.Host = host
s.Server.Address.Port = uint16(port)
}
host, port, err := net.SplitHostPort(address)
if err != nil {
log.Fatal(err.Error())
}

value, err := strconv.ParseUint(port, 0, 16)
if err != nil {
log.Fatal(err.Error())
}

s.Grpc = &GrpcConfig{
Address: Address{
Host: host,
Port: uint16(value),
},
}
}
}

func withTrustedSubnet(value string) Option {
return func(s *Config) {
s.Server.TrustedSubnet = &value
}
}

func withStoreFile(value string) Option {
return func(s *Config) {
s.Store.StoreFile = &value
Expand Down
4 changes: 3 additions & 1 deletion config/enviroment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type EnvironmentVariables map[Variable]func(string) Option
// NewEnvironmentVariables creates EnvironmentVariables struct
func NewEnvironmentVariables() EnvironmentVariables {
return EnvironmentVariables{
newVariable("ADDRESS", "a"): withAddress,
newVariable("ADDRESS", "a"): withHTTPAddress,
newVariable("REPORT_INTERVAL", "ri"): withReportInterval,
newVariable("POLL_INTERVAL", "p"): withPollInterval,
newVariable("CLIENT_TIMEOUT", "c"): withClientTimeout,
Expand All @@ -28,6 +28,8 @@ func NewEnvironmentVariables() EnvironmentVariables {
newVariable("STORE_FILE", "f"): withStoreFile,
newVariable("DATABASE_DSN", "d"): withDatabase,
newVariable("CRYPTO_KEY", "crypto-key"): withCryptoKey,
newVariable("TRUSTED_SUBNET", "t"): withTrustedSubnet,
newVariable("GRPC_PORT", "grpc-port"): withGrpcAddress,
}
}

Expand Down
2 changes: 1 addition & 1 deletion config/enviroment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestEnvironment(t *testing.T) {
config := Config{}
options := NewEnvironmentVariables().Options()
config.SetFromOptions(options...)
assert.Equal(t, address, config.Address())
assert.Equal(t, address, config.Server.Address.String())
assert.Equal(t, duration, config.Agent.ReportInterval)
assert.Equal(t, duration, config.Agent.PollInterval)
assert.Equal(t, duration, config.Agent.ClientTimeout)
Expand Down
7 changes: 6 additions & 1 deletion develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ store:
database: null #"postgres://postgres:postgres@localhost/postgres?sslmode=disable"
store_file: "/tmp/devops-metrics-db.json"
restore: true
store_interval: 5m0s
store_interval: 5m0s

grpc:
address:
host: 0.0.0.0
port: 3200
27 changes: 27 additions & 0 deletions develop_grpc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"server": {
"address": {
"host": "0.0.0.0",
"port": 8080
},
"key": null,
"trusted_subnet": "0.0.0.0/24"
},
"agent": {
"poll_interval": 2000000000,
"report_interval": 3000000000,
"client_timeout": 1000000000
},
"store": {
"database": null,
"store_file": "/tmp/devops-metrics-db.json",
"restore": true,
"store_interval": 300000000000
},
"grpc": {
"address": {
"host": "0.0.0.0",
"port": 3020
}
}
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ require (
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.6
github.com/shirou/gopsutil/v3 v3.22.6
github.com/stretchr/testify v1.7.5
github.com/stretchr/testify v1.8.1
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/net v0.4.0 // indirect
google.golang.org/genproto v0.0.0-20221207170731-23e4bf6bdc37 // indirect
google.golang.org/grpc v1.51.0
google.golang.org/protobuf v1.28.1
gopkg.in/yaml.v3 v3.0.1
)
Loading