mirror of
https://github.com/mentos1386/zdravko.git
synced 2024-11-28 10:11:17 +00:00
47 lines
997 B
Go
47 lines
997 B
Go
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
|
|
v.SetDefault("token", os.Getenv("WORKER_GROUP_TOKEN"))
|
|
v.SetDefault("apiurl", GetEnvOrDefault("WORKER_API_URL", "http://localhost:8000"))
|
|
|
|
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
|
|
}
|