diff --git a/cmd/proxy/main.go b/cmd/proxy/main.go index 80cbe62..0efb456 100644 --- a/cmd/proxy/main.go +++ b/cmd/proxy/main.go @@ -18,7 +18,14 @@ const svcName = "proxy" func main() { g, ctx := errgroup.WithContext(context.Background()) - logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) + var level slog.Level + if err := level.UnmarshalText([]byte("info")); err != nil { + log.Fatalf("failed to parse log level: %s", err.Error()) + } + logHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ + Level: level, + }) + logger := slog.New(logHandler) slog.SetDefault(logger) mqttCfg := config.MQTTProxyConfig{} @@ -26,23 +33,11 @@ func main() { log.Fatalf("failed to load mqtt config : %s", err.Error()) } - if err := mqttCfg.Validate(); err != nil { - log.Fatalf("failed to validate mqtt config : %s", err.Error()) - - return - } - httpCfg := config.HTTPProxyConfig{} if err := env.Parse(&httpCfg); err != nil { log.Fatalf("failed to load http config : %s", err.Error()) } - if err := httpCfg.Validate(); err != nil { - log.Fatalf("failed to validate http config : %s", err.Error()) - - return - } - logger.Info("successfully initialized MQTT and HTTP config") service, err := proxy.NewService(ctx, &mqttCfg, &httpCfg, logger) diff --git a/proxy/config/http.go b/proxy/config/http.go index a17a404..8b9901c 100644 --- a/proxy/config/http.go +++ b/proxy/config/http.go @@ -7,7 +7,6 @@ import ( "fmt" "io" "log" - "net/url" "github.com/absmach/propeller/proplet" ocispec "github.com/opencontainers/image-spec/specs-go/v1" @@ -22,36 +21,12 @@ const ( ) type HTTPProxyConfig struct { - RegistryURL string `env:"PROXY_REGISTRY_URL" envDefault:""` - ChunkSize int `env:"PROXY_CHUNK_SIZE" envDefault:"512000"` - Authenticate bool `env:"PROXY_AUTHENTICATE" envDefault:"false"` - Token string `env:"PROXY_REGISTRY_TOKEN" envDefault:""` - Username string `env:"PROXY_REGISTRY_USERNAME" envDefault:""` - Password string `env:"PROXY_REGISTRY_PASSWORD" envDefault:""` -} - -func (c *HTTPProxyConfig) Validate() error { - if c.RegistryURL == "" { - return errors.New("broker_url is required") - } - if _, err := url.Parse(c.RegistryURL); err != nil { - return fmt.Errorf("broker_url is not a valid URL: %w", err) - } - - if c.Authenticate { - hasToken := c.Token != "" - hasCredentials := c.Username != "" && c.Password != "" - - if !hasToken && !hasCredentials { - return errors.New("either PAT or username/password must be provided when authentication is enabled") - } - - if hasToken && c.Username == "" { - return errors.New("username is required when using PAT authentication") - } - } - - return nil + RegistryURL string `env:"PROXY_REGISTRY_URL,notEmpty" envDefault:""` + ChunkSize int `env:"PROXY_CHUNK_SIZE" envDefault:"512000"` + Authenticate bool `env:"PROXY_AUTHENTICATE" envDefault:"false"` + Token string `env:"PROXY_REGISTRY_TOKEN" envDefault:""` + Username string `env:"PROXY_REGISTRY_USERNAME" envDefault:""` + Password string `env:"PROXY_REGISTRY_PASSWORD" envDefault:""` } func (c *HTTPProxyConfig) setupAuthentication(repo *remote.Repository) { diff --git a/proxy/config/mqtt.go b/proxy/config/mqtt.go index 9db9e36..30031aa 100644 --- a/proxy/config/mqtt.go +++ b/proxy/config/mqtt.go @@ -1,34 +1,8 @@ package config -import ( - "errors" - "fmt" - "net/url" -) - type MQTTProxyConfig struct { - BrokerURL string `env:"PROXY_MQTT_ADDRESS" envDefault:"tcp://localhost:1883"` - Password string `env:"PROXY_PROPLET_KEY" envDefault:""` - PropletID string `env:"PROXY_PROPLET_ID" envDefault:""` - ChannelID string `env:"PROXY_CHANNEL_ID" envDefault:""` -} - -func (c *MQTTProxyConfig) Validate() error { - if c.BrokerURL == "" { - return errors.New("broker_url is required") - } - if _, err := url.Parse(c.BrokerURL); err != nil { - return fmt.Errorf("broker_url is not a valid URL: %w", err) - } - if c.Password == "" { - return errors.New("password is required") - } - if c.PropletID == "" { - return errors.New("proplet_id is required") - } - if c.ChannelID == "" { - return errors.New("channel_id is required") - } - - return nil + BrokerURL string `env:"PROXY_MQTT_ADDRESS" envDefault:"tcp://localhost:1883"` + Password string `env:"PROXY_PROPLET_KEY,notEmpty" envDefault:""` + PropletID string `env:"PROXY_PROPLET_ID,notEmpty" envDefault:""` + ChannelID string `env:"PROXY_CHANNEL_ID,notEmpty" envDefault:""` }