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.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
new file mode 100644
index 0000000..7ccad85
--- /dev/null
+++ b/internal/config/config.go
@@ -0,0 +1,72 @@
+package config
+
+import (
+	"io/fs"
+	"net/url"
+	"path/filepath"
+
+	"go.alanpearce.eu/x/log"
+
+	"github.com/BurntSushi/toml"
+	"gitlab.com/tozd/go/errors"
+)
+
+type Taxonomy struct {
+	Name string
+	Feed bool
+}
+
+type MenuItem struct {
+	Name string
+	URL  URL `toml:"url"`
+}
+
+type URL struct {
+	*url.URL
+}
+
+func (u *URL) UnmarshalText(text []byte) (err error) {
+	u.URL, err = url.Parse(string(text))
+
+	return errors.Wrapf(err, "could not parse URL %s", string(text))
+}
+
+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"`
+	GoatCounter      URL    `toml:"goatcounter"`
+	Domains          []string
+	WildcardDomain   string `toml:"wildcard_domain"`
+	OIDCHost         URL    `toml:"oidc_host"`
+	Taxonomies       []Taxonomy
+	CSP              *CSP `toml:"content-security-policy"`
+	Extra            struct {
+		Headers map[string]string
+	}
+	Menus map[string][]MenuItem
+}
+
+func GetConfig(dir string, log *log.Logger) (*Config, error) {
+	config := Config{}
+	filename := filepath.Join(dir, "config.toml")
+	log.Debug("reading config", "filename", filename)
+	_, err := toml.DecodeFile(filename, &config)
+	if err != nil {
+		switch t := err.(type) {
+		case *fs.PathError:
+			return nil, errors.WithMessage(t, "could not read configuration")
+		case *toml.ParseError:
+			return nil, errors.WithMessage(t, t.ErrorWithUsage())
+		}
+
+		return nil, errors.Wrap(err, "config error")
+	}
+
+	return &config, nil
+}