aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/config/main.go
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.net>2025-07-08 23:26:05 +0100
committerLeonardo Bishop <me@leonardobishop.net>2025-07-08 23:26:05 +0100
commitcdb75d3fcbc9339b897f8c6ff4d69a577f017393 (patch)
tree5e757cd236540c2cea9874c1bc09f19548db05d5 /pkg/config/main.go
parentb56101f1a11552067f594679a497ebd4cf7427d4 (diff)
Rewrite in Go
Diffstat (limited to 'pkg/config/main.go')
-rw-r--r--pkg/config/main.go45
1 files changed, 45 insertions, 0 deletions
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)
+}