2024-02-19 09:09:30 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WorkerConfig struct {
|
|
|
|
Token string `validate:"required"`
|
|
|
|
ApiUrl string `validate:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWorkerConfig() *WorkerConfig {
|
|
|
|
v := newViper()
|
|
|
|
|
|
|
|
// Set defaults
|
2024-02-24 21:07:49 +00:00
|
|
|
v.SetDefault("token", os.Getenv("WORKER_GROUP_TOKEN"))
|
2024-02-19 09:17:32 +00:00
|
|
|
v.SetDefault("apiurl", GetEnvOrDefault("WORKER_API_URL", "http://localhost:8000"))
|
2024-02-19 09:09:30 +00:00
|
|
|
|
|
|
|
err := v.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
|
|
// ignore
|
|
|
|
} else {
|
|
|
|
log.Fatalf("Error reading config file, %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Println("Config file used: ", v.ConfigFileUsed())
|
|
|
|
|
|
|
|
config := &WorkerConfig{}
|
|
|
|
err = v.Unmarshal(config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error unmarshalling config, %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate config
|
|
|
|
validate := validator.New(validator.WithRequiredStructEnabled())
|
|
|
|
err = validate.Struct(config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error validating config, %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|