aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/config
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
parentb56101f1a11552067f594679a497ebd4cf7427d4 (diff)
Rewrite in Go
Diffstat (limited to 'pkg/config')
-rw-r--r--pkg/config/main.go45
-rw-r--r--pkg/config/site.go35
2 files changed, 80 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)
+}
diff --git a/pkg/config/site.go b/pkg/config/site.go
new file mode 100644
index 0000000..ffd5ddc
--- /dev/null
+++ b/pkg/config/site.go
@@ -0,0 +1,35 @@
+package config
+
+import (
+ "os"
+
+ "github.com/pelletier/go-toml/v2"
+)
+
+type SiteConfig struct {
+ Host string
+}
+
+func ReadSiteConfig(filePath string, dst *SiteConfig) error {
+ config, err := os.ReadFile(filePath)
+ if err != nil {
+ return err
+ }
+
+ if err := toml.Unmarshal(config, dst); err != nil {
+ return err
+ }
+ return nil
+}
+
+func WriteSiteConfig(filePath string, src *SiteConfig) error {
+ config, err := toml.Marshal(src)
+ if err != nil {
+ return err
+ }
+
+ if err := os.WriteFile(filePath, config, 0o644); err != nil {
+ return err
+ }
+ return nil
+}