diff options
author | Alan Pearce | 2024-05-09 16:24:45 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-09 16:27:19 +0200 |
commit | 967f6fdf5c1693d3aa27079b3ae28768fb7356c6 (patch) | |
tree | e878af655584faca476f4ba2cf8d96cba7383d7e | |
parent | 453ae8569ab58fcc4ad61c461adc4489b9443cf8 (diff) | |
download | searchix-967f6fdf5c1693d3aa27079b3ae28768fb7356c6.tar.lz searchix-967f6fdf5c1693d3aa27079b3ae28768fb7356c6.tar.zst searchix-967f6fdf5c1693d3aa27079b3ae28768fb7356c6.zip |
feat: make configuration optional
-rw-r--r-- | config.toml | 20 | ||||
-rw-r--r-- | internal/config/config.go | 41 | ||||
-rw-r--r-- | internal/file/utils.go | 53 |
3 files changed, 80 insertions, 34 deletions
diff --git a/config.toml b/config.toml deleted file mode 100644 index 9ecaa6e..0000000 --- a/config.toml +++ /dev/null @@ -1,20 +0,0 @@ -base_url = "https://searchix.alanpearce.eu" -data_path = "./data" - -[content-security-policy] -default-src = [ - "'self'", -] -image-src = [ - "http://gc.zgo.at", -] -script-src = [ - "'self'", - "http://gc.zgo.at", -] -require-trusted-types-for = [ - "'script'", -] - -[headers] -x-content-type-options = "nosniff" diff --git a/internal/config/config.go b/internal/config/config.go index c721424..5b06efa 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,6 +4,7 @@ import ( "log/slog" "net/url" "os" + "searchix/internal/file" "github.com/pelletier/go-toml/v2" "github.com/pkg/errors" @@ -24,28 +25,40 @@ func (u *URL) UnmarshalText(text []byte) (err error) { } type Config struct { - BaseURL URL `toml:"base_url"` DataPath string `toml:"data_path"` - CSP *CSP `toml:"content-security-policy"` + 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 := Config{} + config := defaultConfig slog.Debug("reading config.toml") - file, err := os.Open("config.toml") - if err != nil { - return nil, errors.WithMessage(err, "error reading configuration") + f, err := os.Open("config.toml") + if err := file.NeedNotExist(err); err != nil { + return nil, errors.Wrap(err, "reading config.toml failed") } - dec := toml.NewDecoder(file) - err = dec.Decode(&config) - if err != nil { - var tomlError toml.DecodeError - if errors.As(err, &tomlError) { - return nil, errors.WithMessage(err, tomlError.Error()) - } + 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 nil, errors.Wrap(err, "config error") + } } return &config, nil diff --git a/internal/file/utils.go b/internal/file/utils.go new file mode 100644 index 0000000..efcf0f6 --- /dev/null +++ b/internal/file/utils.go @@ -0,0 +1,53 @@ +package file + +import ( + "io" + "io/fs" + "os" + + "github.com/pkg/errors" +) + +func Mkdirp(dir string) error { + err := os.MkdirAll(dir, os.ModeDir|os.ModePerm) + if err != nil { + return errors.WithMessagef(err, "could not create directory %s", dir) + } + + return nil +} + +func NeedNotExist(err error) error { + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return err + } + + return nil +} + +func StatIfExists(file string) (fs.FileInfo, error) { + stat, err := os.Stat(file) + + return stat, NeedNotExist(err) +} + +func Exists(file string) (bool, error) { + stat, err := StatIfExists(file) + + return stat != nil, err +} + +func WriteToFile(path string, body io.Reader) error { + file, err := os.Create(path) + if err != nil { + return errors.WithMessagef(err, "error creating file at %s", path) + } + defer file.Close() + + _, err = io.Copy(file, body) + if err != nil { + return errors.WithMessagef(err, "error downloading file %s", path) + } + + return nil +} |