1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package config
import (
"log/slog"
"net/url"
"os"
"searchix/internal/file"
"github.com/pelletier/go-toml/v2"
"github.com/pkg/errors"
)
type URL struct {
*url.URL
}
func (u *URL) UnmarshalText(text []byte) (err error) {
u.URL, err = url.Parse(string(text))
if err != nil {
return errors.WithMessage(err, "could not parse URL")
}
return nil
}
type Config struct {
DataPath string `toml:"data_path"`
CSP CSP `toml:"content-security-policy"`
Headers map[string]string
}
var defaultConfig = Config{
DataPath: "./data",
CSP: CSP{
DefaultSrc: []string{"'self'"},
},
Headers: map[string]string{
"x-content-type-options": "nosniff",
},
}
func GetConfig() (*Config, error) {
config := defaultConfig
slog.Debug("reading config.toml")
f, err := os.Open("config.toml")
if err := file.NeedNotExist(err); err != nil {
return nil, errors.Wrap(err, "reading config.toml failed")
}
if f != nil {
defer f.Close()
dec := toml.NewDecoder(f)
err = dec.Decode(&config)
if err != nil {
var tomlError toml.DecodeError
if errors.As(err, &tomlError) {
return nil, errors.WithMessage(err, tomlError.Error())
}
return nil, errors.Wrap(err, "config error")
}
}
return &config, nil
}
|