From cdb75d3fcbc9339b897f8c6ff4d69a577f017393 Mon Sep 17 00:00:00 2001 From: Leonardo Bishop Date: Tue, 8 Jul 2025 23:26:05 +0100 Subject: Rewrite in Go --- pkg/config/main.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 pkg/config/main.go (limited to 'pkg/config/main.go') diff --git a/pkg/config/main.go b/pkg/config/main.go new file mode 100644 index 0000000..496adf1 --- /dev/null +++ b/pkg/config/main.go @@ -0,0 +1,45 @@ +package config + +import ( + "os" + + "github.com/go-playground/validator/v10" + "github.com/pelletier/go-toml/v2" +) + +type MainConfig struct { + Listen struct { + Address string `validate:"required,ip"` + Port uint16 `validate:"required"` + } + + Command struct { + Host string + Secret string + + API struct { + Enable bool + } + + Web struct { + Enable bool + } + } +} + +func ReadMainConfig(filePath string, dst *MainConfig) error { + config, err := os.ReadFile(filePath) + if err != nil { + return err + } + + if err := toml.Unmarshal(config, dst); err != nil { + return err + } + return nil +} + +func ValidateMainConfig(cfg *MainConfig) error { + validate := validator.New(validator.WithRequiredStructEnabled()) + return validate.Struct(cfg) +} -- cgit v1.2.3-70-g09d2