Skip to content

Commit

Permalink
Merge pull request #26 from vindosVP/iter25
Browse files Browse the repository at this point in the history
Iter25
  • Loading branch information
vindosVP committed Jun 10, 2024
2 parents 2a0ddf4 + 1469112 commit 77e851d
Show file tree
Hide file tree
Showing 17 changed files with 1,753 additions and 169 deletions.
20 changes: 20 additions & 0 deletions cmd/agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type AgentConfig struct {
Config string
UseRPC bool
ServerAddr string
LogLevel string
Key string
Expand All @@ -22,6 +23,7 @@ type AgentConfig struct {

type tempConfig struct {
Config string `json:"-"`
UseRPC bool `json:"use_rpc"`
ServerAddr string `json:"address"`
LogLevel string `json:"log_level"`
Key string `json:"key"`
Expand All @@ -33,6 +35,7 @@ type tempConfig struct {

type configFullness struct {
Config bool
UseRPC bool
ServerAddr bool
LogLevel bool
Key bool
Expand Down Expand Up @@ -62,6 +65,14 @@ func parseEnvs(config *AgentConfig, full *configFullness) {
config.ServerAddr = val
full.ServerAddr = true
}
if val, ok := os.LookupEnv("USE_RPC"); ok {
l, err := strconv.ParseBool(val)
if err != nil {
log.Fatalf("Failed to parse env USE_RPC value: %v", err)
}
config.UseRPC = l
full.UseRPC = true
}
if val, ok := os.LookupEnv("LOG_LEVEL"); ok {
config.LogLevel = val
full.LogLevel = true
Expand Down Expand Up @@ -110,6 +121,10 @@ func parseFlags(config *AgentConfig, full *configFullness) {
config.ServerAddr = flagCfg.ServerAddr
full.ServerAddr = true
}
if !full.UseRPC {
config.UseRPC = flagCfg.UseRPC
full.UseRPC = true
}
if !full.LogLevel && flagCfg.LogLevel != "" {
config.LogLevel = flagCfg.LogLevel
full.LogLevel = true
Expand Down Expand Up @@ -151,6 +166,10 @@ func parseJSON(config *AgentConfig, full *configFullness) {
config.ServerAddr = JSONCfg.ServerAddr
full.ServerAddr = true
}
if !full.UseRPC {
config.UseRPC = JSONCfg.UseRPC
full.UseRPC = true
}
if !full.LogLevel && JSONCfg.LogLevel != "" {
config.LogLevel = JSONCfg.LogLevel
full.LogLevel = true
Expand Down Expand Up @@ -180,6 +199,7 @@ func parseJSON(config *AgentConfig, full *configFullness) {
func flags() *tempConfig {
flagConfig := &tempConfig{}
flag.StringVar(&flagConfig.ServerAddr, "a", "localhost:8080", "metrics server address")
flag.BoolVar(&flagConfig.UseRPC, "rpc", true, "use rpc")
flag.IntVar(&flagConfig.PollInterval, "p", 2, "metrics poll interval")
flag.IntVar(&flagConfig.ReportInterval, "r", 10, "report interval")
flag.StringVar(&flagConfig.LogLevel, "lg", "info", "log level")
Expand Down
36 changes: 35 additions & 1 deletion cmd/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type ServerConfig struct {
Config string
RPCAddr string
RunAddr string
LogLevel string
FileStoragePath string
Expand All @@ -19,29 +20,34 @@ type ServerConfig struct {
StoreInterval time.Duration
Restore bool
CryptoKeyFile string
TrustedSubnet string
}

type tempConfig struct {
Config string
RunAddr string
RPCAddr string
LogLevel string
FileStoragePath string
DatabaseDNS string
Key string
StoreInterval int
Restore bool
CryptoKeyFile string
TrustedSubnet string
}

type jsonConfig struct {
RunAddr string `json:"address"`
RPCAddr string `json:"rpc_address"`
LogLevel string `json:"log_level"`
FileStoragePath string `json:"store_file"`
DatabaseDNS string `json:"database_dsn"`
Key string `json:"key"`
StoreInterval int `json:"store_interval"`
Restore bool `json:"restore"`
CryptoKeyFile string `json:"crypto_key"`
TrustedSubnet string `json:"trusted_subnet"`
}

type configFullness struct {
Expand All @@ -54,6 +60,8 @@ type configFullness struct {
CryptoKeyFile bool
StoreInterval bool
Restore bool
TrustedSubnet bool
RPCAddr bool
}

func NewServerConfig() *ServerConfig {
Expand All @@ -77,6 +85,10 @@ func parseFlags(config *ServerConfig, full *configFullness) {
config.RunAddr = flagCfg.RunAddr
full.RunAddr = true
}
if !full.RPCAddr && flagCfg.RPCAddr != "" {
config.RPCAddr = flagCfg.RPCAddr
full.RPCAddr = true
}
if !full.LogLevel && flagCfg.LogLevel != "" {
config.LogLevel = flagCfg.LogLevel
full.LogLevel = true
Expand All @@ -101,6 +113,10 @@ func parseFlags(config *ServerConfig, full *configFullness) {
config.Restore = flagCfg.Restore
full.Restore = true
}
if !full.TrustedSubnet && flagCfg.TrustedSubnet != "" {
config.TrustedSubnet = flagCfg.TrustedSubnet
full.TrustedSubnet = true
}
if !full.CryptoKeyFile && flagCfg.CryptoKeyFile != "" {
config.CryptoKeyFile = flagCfg.CryptoKeyFile
full.CryptoKeyFile = true
Expand All @@ -109,7 +125,8 @@ func parseFlags(config *ServerConfig, full *configFullness) {

func parseFlagConfig() *tempConfig {
flagConfig := &tempConfig{}
flag.StringVar(&flagConfig.RunAddr, "a", "localhost:8080", "address and port to run server")
flag.StringVar(&flagConfig.RunAddr, "a", "localhost:8080", "address and port to run http server")
flag.StringVar(&flagConfig.RPCAddr, "ra", "localhost:9090", "address and port to run rpc server")
flag.StringVar(&flagConfig.LogLevel, "l", "info", "log level")
flag.StringVar(&flagConfig.FileStoragePath, "f", "./tmp/metrics-db.json", "file storage path")
flag.IntVar(&flagConfig.StoreInterval, "i", 300, "store interval")
Expand All @@ -118,6 +135,7 @@ func parseFlagConfig() *tempConfig {
flag.StringVar(&flagConfig.Key, "k", "", "hash key")
flag.StringVar(&flagConfig.CryptoKeyFile, "crypto-key", "", "crypto key")
flag.StringVar(&flagConfig.Config, "c", "", "json config file")
flag.StringVar(&flagConfig.TrustedSubnet, "t", "", "trusted subnet")
flag.Parse()
return flagConfig
}
Expand All @@ -131,6 +149,10 @@ func parseEnvs(config *ServerConfig, full *configFullness) {
config.RunAddr = val
full.RunAddr = true
}
if val, ok := os.LookupEnv("RPC_ADDRESS"); ok {
config.RPCAddr = val
full.RPCAddr = true
}
if val, ok := os.LookupEnv("LOG_LEVEL"); ok {
config.LogLevel = val
full.LogLevel = true
Expand All @@ -151,6 +173,10 @@ func parseEnvs(config *ServerConfig, full *configFullness) {
config.CryptoKeyFile = val
full.CryptoKeyFile = true
}
if val, ok := os.LookupEnv("TRUSTED_SUBNET"); ok {
config.TrustedSubnet = val
full.TrustedSubnet = true
}
if val, ok := os.LookupEnv("STORE_INTERVAL"); ok {
storeInterval, err := strconv.Atoi(val)
if err != nil {
Expand Down Expand Up @@ -188,6 +214,10 @@ func parseJSON(config *ServerConfig, full *configFullness) {
config.LogLevel = JSONCfg.LogLevel
full.LogLevel = true
}
if !full.RPCAddr && JSONCfg.RPCAddr != "" {
config.RPCAddr = JSONCfg.RPCAddr
full.RPCAddr = true
}
if !full.FileStoragePath && JSONCfg.FileStoragePath != "" {
config.FileStoragePath = JSONCfg.FileStoragePath
full.FileStoragePath = true
Expand All @@ -212,4 +242,8 @@ func parseJSON(config *ServerConfig, full *configFullness) {
config.CryptoKeyFile = JSONCfg.CryptoKeyFile
full.CryptoKeyFile = true
}
if !full.TrustedSubnet {
config.TrustedSubnet = JSONCfg.TrustedSubnet
full.TrustedSubnet = true
}
}
4 changes: 3 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ func main() {
if err != nil {
log.Fatalf(fmt.Sprintf("Failed to initialize logger: %v", err))
}
err = server.Run(cfg)
logger.Log.Info("Starting server")
s, err := server.New(cfg)
if err != nil {
logger.Log.Fatal(fmt.Sprintf("Failed to start server: %v", err))
}
s.Run()
}

func printBuildInfo() {
Expand Down
12 changes: 8 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module github.com/vindosVP/metrics

go 1.20
go 1.21

toolchain go1.21.8

require (
github.com/avast/retry-go/v4 v4.5.1
github.com/caarlos0/env/v10 v10.0.0
github.com/go-chi/chi/v5 v5.0.11
github.com/go-resty/resty/v2 v2.11.0
github.com/gostaticanalysis/sqlrows v0.0.0-20231116101209-5091a5920ea6
github.com/gostaticanalysis/wraperrfmt v0.0.0-20240424134522-a58689aa692e
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
github.com/jackc/pgx/v5 v5.5.3
github.com/jarcoal/httpmock v1.3.1
Expand All @@ -17,6 +17,8 @@ require (
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
golang.org/x/tools v0.20.0
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.1
honnef.co/go/tools v0.4.7
)

Expand All @@ -28,6 +30,7 @@ require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
Expand All @@ -44,6 +47,7 @@ require (
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 77e851d

Please sign in to comment.