Skip to content

Commit

Permalink
revert to a toml file
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <[email protected]>
  • Loading branch information
nyagamunene committed Jan 14, 2025
1 parent ea84ec3 commit c39ee19
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 448 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Propellerd Build
build
config.toml

.env
38 changes: 19 additions & 19 deletions cli/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,22 @@ var provisionCmd = &cobra.Command{
PropletChannel: managerChannel,
}

configContent := fmt.Sprintf(`# SuperMQ Environment Configuration
# Manager Configuration
MANAGER_THING_ID=%s
MANAGER_THING_KEY=%s
MANAGER_CHANNEL_ID=%s
# Proplet Configuration
PROPLET_THING_ID=%s
PROPLET_THING_KEY=%s
PROPLET_CHANNEL_ID=%s
# Proxy Configuration
PROXY_THING_ID=%s
PROXY_THING_KEY=%s
PROXY_CHANNEL_ID=%s`,
configContent := fmt.Sprintf(`# SuperMQ Configuration
[manager]
thing_id = "%s"
thing_key = "%s"
channel_id = "%s"
[proplet]
thing_id = "%s"
thing_key = "%s"
channel_id = "%s"
[proxy]
thing_id = "%s"
thing_key = "%s"
channel_id = "%s"`,
managerThing.ID,
managerThing.Credentials.Secret,
managerChannel.ID,
Expand All @@ -164,12 +164,12 @@ PROXY_CHANNEL_ID=%s`,
managerChannel.ID,
)

if err := os.WriteFile(".env", []byte(configContent), filePermission); err != nil {
logErrorCmd(*cmd, errors.New("failed to create .env file"))
if err := os.WriteFile("config.toml", []byte(configContent), filePermission); err != nil {
logErrorCmd(*cmd, errors.New("failed to create config.toml file"))

return
}
logSuccessCmd(*cmd, "Successfully created .env file")
logSuccessCmd(*cmd, "Successfully created config.toml file")

logJSONCmd(*cmd, res)
},
Expand Down
27 changes: 18 additions & 9 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
"github.com/absmach/propeller/manager"
"github.com/absmach/propeller/manager/api"
"github.com/absmach/propeller/manager/middleware"
"github.com/absmach/propeller/pkg/config"
"github.com/absmach/propeller/pkg/mqtt"
"github.com/absmach/propeller/pkg/scheduler"
"github.com/absmach/propeller/pkg/storage"
"github.com/caarlos0/env/v11"
"github.com/google/uuid"
"github.com/joho/godotenv"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
"golang.org/x/sync/errgroup"
Expand All @@ -31,7 +31,7 @@ const (
svcName = "manager"
defHTTPPort = "7070"
envPrefixHTTP = "MANAGER_HTTP_"
pathEnv = ".env"
configPath = "config.toml"
)

type envConfig struct {
Expand All @@ -40,9 +40,6 @@ type envConfig struct {
MQTTAddress string `env:"MANAGER_MQTT_ADDRESS" envDefault:"tcp://localhost:1883"`
MQTTQoS uint8 `env:"MANAGER_MQTT_QOS" envDefault:"2"`
MQTTTimeout time.Duration `env:"MANAGER_MQTT_TIMEOUT" envDefault:"30s"`
ThingID string `env:"MANAGER_THING_ID"`
ThingKey string `env:"MANAGER_THING_KEY"`
ChannelID string `env:"MANAGER_CHANNEL_ID"`
Server server.Config
OTELURL url.URL `env:"MANAGER_OTEL_URL"`
TraceRatio float64 `env:"MANAGER_TRACE_RATIO" envDefault:"0"`
Expand All @@ -52,8 +49,13 @@ func main() {
ctx, cancel := context.WithCancel(context.Background())
g, ctx := errgroup.WithContext(ctx)

if _, err := os.Stat(pathEnv); err == nil {
_ = godotenv.Load(pathEnv)
var conf *config.Config
if _, err := os.Stat(configPath); err == nil {
var err error
conf, err = config.LoadConfig(configPath)
if err != nil {
log.Fatalf("failed to load TOML configuration: %s", err.Error())
}
}

cfg := envConfig{}
Expand Down Expand Up @@ -95,7 +97,14 @@ func main() {
}
tracer := tp.Tracer(svcName)

mqttPubSub, err := mqtt.NewPubSub(cfg.MQTTAddress, cfg.MQTTQoS, svcName, cfg.ThingID, cfg.ThingKey, cfg.ChannelID, cfg.MQTTTimeout, logger)
var thingID, thingKey, channelID string
if conf != nil {
thingID = conf.Manager.ThingID
thingKey = conf.Manager.ThingKey
channelID = conf.Manager.ChannelID
}

mqttPubSub, err := mqtt.NewPubSub(cfg.MQTTAddress, cfg.MQTTQoS, svcName, thingID, thingKey, channelID, cfg.MQTTTimeout, logger)
if err != nil {
logger.Error("failed to initialize mqtt pubsub", slog.String("error", err.Error()))

Expand All @@ -108,7 +117,7 @@ func main() {
storage.NewInMemoryStorage(),
scheduler.NewRoundRobin(),
mqttPubSub,
cfg.ChannelID,
channelID,
logger,
)
svc = middleware.Logging(logger, svc)
Expand Down
31 changes: 20 additions & 11 deletions cmd/proplet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
"time"

"github.com/absmach/magistrala/pkg/server"
"github.com/absmach/propeller/pkg/config"
"github.com/absmach/propeller/pkg/mqtt"
"github.com/absmach/propeller/proplet"
"github.com/caarlos0/env/v11"
"github.com/google/uuid"
"github.com/joho/godotenv"
"golang.org/x/sync/errgroup"
)

const (
svcName = "proplet"
pathEnv = ".env"
svcName = "proplet"
configPath = "config.toml"
)

type envConfig struct {
Expand All @@ -29,17 +29,19 @@ type envConfig struct {
MQTTTimeout time.Duration `env:"PROPLET_MQTT_TIMEOUT" envDefault:"30s"`
MQTTQoS byte `env:"PROPLET_MQTT_QOS" envDefault:"2"`
LivelinessInterval time.Duration `env:"PROPLET_LIVELINESS_INTERVAL" envDefault:"10s"`
ThingID string `env:"PROPLET_THING_ID"`
ThingKey string `env:"PROPLET_THING_KEY"`
ChannelID string `env:"PROPLET_CHANNEL_ID"`
}

func main() {
ctx, cancel := context.WithCancel(context.Background())
g, ctx := errgroup.WithContext(ctx)

if _, err := os.Stat(pathEnv); err == nil {
_ = godotenv.Load(pathEnv)
var conf *config.Config
if _, err := os.Stat(configPath); err == nil {
var err error
conf, err = config.LoadConfig(configPath)
if err != nil {
log.Fatalf("failed to load TOML configuration: %s", err.Error())
}
}

cfg := envConfig{}
Expand All @@ -61,15 +63,22 @@ func main() {
logger := slog.New(logHandler)
slog.SetDefault(logger)

mqttPubSub, err := mqtt.NewPubSub(cfg.MQTTAddress, cfg.MQTTQoS, cfg.InstanceID, cfg.ThingID, cfg.ThingKey, cfg.ChannelID, cfg.MQTTTimeout, logger)
var thingID, thingKey, channelID string
if conf != nil {
thingID = conf.Proplet.ThingID
thingKey = conf.Proplet.ThingKey
channelID = conf.Proplet.ChannelID
}

mqttPubSub, err := mqtt.NewPubSub(cfg.MQTTAddress, cfg.MQTTQoS, cfg.InstanceID, thingID, thingKey, channelID, cfg.MQTTTimeout, logger)
if err != nil {
logger.Error("failed to initialize mqtt client", slog.Any("error", err))

return
}
wazero := proplet.NewWazeroRuntime(logger, mqttPubSub, cfg.ChannelID)
wazero := proplet.NewWazeroRuntime(logger, mqttPubSub, channelID)

service, err := proplet.NewService(ctx, cfg.ChannelID, cfg.ThingID, cfg.ThingKey, cfg.LivelinessInterval, mqttPubSub, logger, wazero)
service, err := proplet.NewService(ctx, channelID, thingID, thingKey, cfg.LivelinessInterval, mqttPubSub, logger, wazero)
if err != nil {
logger.Error("failed to initialize service", slog.Any("error", err))

Expand Down
31 changes: 20 additions & 11 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,21 @@ import (
"os"
"time"

"github.com/absmach/propeller/pkg/config"
"github.com/absmach/propeller/proxy"
"github.com/caarlos0/env/v11"
"github.com/joho/godotenv"
"golang.org/x/sync/errgroup"
)

const (
svcName = "proxy"
pathEnv = ".env"
svcName = "proxy"
configPath = "config.toml"
)

type envConfig struct {
LogLevel string `env:"PROXY_LOG_LEVEL" envDefault:"info"`
MQTTAddress string `env:"PROXY_MQTT_ADDRESS" envDefault:"tcp://localhost:1883"`
MQTTTimeout time.Duration `env:"PROXY_MQTT_TIMEOUT" envDefault:"30s"`
ThingID string `env:"PROXY_THING_ID"`
ThingKey string `env:"PROXY_THING_KEY"`
ChannelID string `env:"PROXY_CHANNEL_ID"`

// HTTP Registry configuration
ChunkSize int `env:"PROXY_CHUNK_SIZE" envDefault:"512000"`
Expand All @@ -39,8 +36,13 @@ type envConfig struct {
func main() {
g, ctx := errgroup.WithContext(context.Background())

if _, err := os.Stat(pathEnv); err == nil {
_ = godotenv.Load(pathEnv)
var conf *config.Config
if _, err := os.Stat(configPath); err == nil {
var err error
conf, err = config.LoadConfig(configPath)
if err != nil {
log.Fatalf("failed to load TOML configuration: %s", err.Error())
}
}

cfg := envConfig{}
Expand All @@ -58,11 +60,18 @@ func main() {
logger := slog.New(logHandler)
slog.SetDefault(logger)

var thingID, thingKey, channelID string
if conf != nil {
thingID = conf.Proxy.ThingID
thingKey = conf.Proxy.ThingKey
channelID = conf.Proxy.ChannelID
}

mqttCfg := proxy.MQTTProxyConfig{
BrokerURL: cfg.MQTTAddress,
Password: cfg.ThingKey,
PropletID: cfg.ThingID,
ChannelID: cfg.ChannelID,
Password: thingKey,
PropletID: thingID,
ChannelID: channelID,
}

httpCfg := proxy.HTTPProxyConfig{
Expand Down
Loading

0 comments on commit c39ee19

Please sign in to comment.