about summary refs log tree commit diff stats
path: root/internal/config/config.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/config/config.go')
-rw-r--r--internal/config/config.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
new file mode 100644
index 0000000..578390e
--- /dev/null
+++ b/internal/config/config.go
@@ -0,0 +1,73 @@
+package config
+
+import (
+	"io/fs"
+	"log/slog"
+	"net/url"
+	"os"
+
+	"github.com/BurntSushi/toml"
+	"github.com/pkg/errors"
+)
+
+type Taxonomy struct {
+	Name string
+	Feed bool
+}
+
+type MenuItem struct {
+	Name string
+	URL  string `toml:"url"`
+}
+
+type URL struct {
+	*url.URL
+}
+
+func (u *URL) UnmarshalText(text []byte) (err error) {
+	u.URL, err = url.Parse(string(text))
+	return err
+}
+
+type Config struct {
+	DefaultLanguage  string `toml:"default_language"`
+	BaseURL          URL    `toml:"base_url"`
+	InjectLiveReload bool
+	Title            string
+	Email            string
+	Description      string
+	DomainStartDate  string `toml:"domain_start_date"`
+	OriginalDomain   string `toml:"original_domain"`
+	Taxonomies       []Taxonomy
+	CSP              *CSP `toml:"content-security-policy"`
+	Extra            struct {
+		Headers map[string]string
+	}
+	Menus map[string][]MenuItem
+}
+
+func getEnvFallback(key, fallback string) string {
+	if value, ok := os.LookupEnv(key); ok {
+		return value
+	} else {
+		return fallback
+	}
+}
+
+func GetConfig() (*Config, error) {
+	config := Config{}
+	slog.Debug("reading config.toml")
+	_, err := toml.DecodeFile("config.toml", &config)
+	if err != nil {
+		var pathError *fs.PathError
+		var tomlError toml.ParseError
+		if errors.As(err, &pathError) {
+			return nil, errors.WithMessage(err, "could not read configuration")
+		} else if errors.As(err, &tomlError) {
+			return nil, errors.WithMessage(err, tomlError.ErrorWithUsage())
+		} else {
+			return nil, errors.Wrap(err, "config error")
+		}
+	}
+	return &config, nil
+}