aboutsummaryrefslogtreecommitdiff
path: root/backend/config.go
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-10-02 18:36:06 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2025-10-02 18:36:06 +0200
commit49766901293631aeabfc96e8d80aba305f420630 (patch)
tree56ada5226ac5d47a8277de00022190ab946490a1 /backend/config.go
parent1168ac60d3a22c0d354f291fd657ad302df6ec05 (diff)
feat(backend): config to set domain
Diffstat (limited to 'backend/config.go')
-rw-r--r--backend/config.go48
1 files changed, 47 insertions, 1 deletions
diff --git a/backend/config.go b/backend/config.go
index b7624fd..98aeebe 100644
--- a/backend/config.go
+++ b/backend/config.go
@@ -1,3 +1,49 @@
package backend
-type Config struct{}
+import (
+ "errors"
+ "log/slog"
+ "os"
+
+ "github.com/pelletier/go-toml/v2"
+)
+
+type Config struct {
+ Domain string `toml:"domain"`
+}
+
+func (c *Config) DefaultValues() {
+ c.Domain = "example.org"
+}
+
+func LoadConfig(path string) (*Config, bool) {
+ b, err := os.ReadFile(path)
+ var config Config
+ if err != nil {
+ if !errors.Is(err, os.ErrNotExist) {
+ slog.Error("reading config file", "error", err)
+ return nil, false
+ }
+ slog.Warn("config file not found", "path", path)
+ slog.Info("creating a new config file", "path", path)
+ config.DefaultValues()
+ b, err = toml.Marshal(&config)
+ if err != nil {
+ slog.Error("marshalling config file", "error", err)
+ return nil, false
+ }
+ err = os.WriteFile(path, b, 0660)
+ if err != nil {
+ slog.Error("writing config file", "error", err, "path", path)
+ } else {
+ slog.Info("config file created", "path", path)
+ }
+ return nil, false
+ }
+ err = toml.Unmarshal(b, &config)
+ if err != nil {
+ slog.Error("unmarshalling config file", "error", err)
+ return nil, false
+ }
+ return &config, true
+}