Skip to content

Commit

Permalink
update env file
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <stevenyaga2014@gmail.com>
  • Loading branch information
nyagamunene committed Dec 19, 2024
1 parent dc3fdab commit 0e2c914
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 74 deletions.
21 changes: 8 additions & 13 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,26 @@ 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{}
if err := env.Parse(&mqttCfg); err != nil {
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)
Expand Down
37 changes: 6 additions & 31 deletions proxy/config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"log"
"net/url"

"github.com/absmach/propeller/proplet"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand All @@ -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) {
Expand Down
34 changes: 4 additions & 30 deletions proxy/config/mqtt.go
Original file line number Diff line number Diff line change
@@ -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:""`
}

0 comments on commit 0e2c914

Please sign in to comment.