diff options
author | Alan Pearce | 2024-05-02 23:18:19 +0200 |
---|---|---|
committer | Alan Pearce | 2024-05-02 23:20:30 +0200 |
commit | 73603079e29bc89c54296a9e12b5a779cd84c023 (patch) | |
tree | 3e5d0c0c87b81a007667fc4b533cb9403675a422 /internal/config | |
parent | 7ad48953a4d9470d2f4fe89343c0b09bff410c58 (diff) | |
download | searchix-73603079e29bc89c54296a9e12b5a779cd84c023.tar.lz searchix-73603079e29bc89c54296a9e12b5a779cd84c023.tar.zst searchix-73603079e29bc89c54296a9e12b5a779cd84c023.zip |
feat: serve a very basic html template
Diffstat (limited to 'internal/config')
-rw-r--r-- | internal/config/config.go | 51 | ||||
-rw-r--r-- | internal/config/csp.go | 45 |
2 files changed, 96 insertions, 0 deletions
diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..e9300ce --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,51 @@ +package config + +import ( + "log/slog" + "net/url" + "os" + + "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 { + BaseURL URL `toml:"base_url"` + CSP *CSP `toml:"content-security-policy"` + Headers map[string]string +} + +func GetConfig() (*Config, error) { + config := Config{} + slog.Debug("reading config.toml") + file, err := os.Open("config.toml") + if err != nil { + return nil, errors.WithMessage(err, "error reading configuration") + } + 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()) + } + + return nil, errors.Wrap(err, "config error") + } + + return &config, nil +} diff --git a/internal/config/csp.go b/internal/config/csp.go new file mode 100644 index 0000000..970663c --- /dev/null +++ b/internal/config/csp.go @@ -0,0 +1,45 @@ +package config + +// Code generated DO NOT EDIT. + +import ( + "github.com/crewjam/csp" +) + +type CSP struct { + BaseURI []string `csp:"base-uri" toml:"base-uri"` + BlockAllMixedContent bool `csp:"block-all-mixed-content" toml:"block-all-mixed-content"` + ChildSrc []string `csp:"child-src" toml:"child-src"` + ConnectSrc []string `csp:"connect-src" toml:"connect-src"` + DefaultSrc []string `csp:"default-src" toml:"default-src"` + FontSrc []string `csp:"font-src" toml:"font-src"` + FormAction []string `csp:"form-action" toml:"form-action"` + FrameAncestors []string `csp:"frame-ancestors" toml:"frame-ancestors"` + FrameSrc []string `csp:"frame-src" toml:"frame-src"` + ImgSrc []string `csp:"img-src" toml:"img-src"` + ManifestSrc []string `csp:"manifest-src" toml:"manifest-src"` + MediaSrc []string `csp:"media-src" toml:"media-src"` + NavigateTo []string `csp:"navigate-to" toml:"navigate-to"` + ObjectSrc []string `csp:"object-src" toml:"object-src"` + PluginTypes []string `csp:"plugin-types" toml:"plugin-types"` + PrefetchSrc []string `csp:"prefetch-src" toml:"prefetch-src"` + Referrer csp.ReferrerPolicy `csp:"referrer" toml:"referrer"` + ReportTo string `csp:"report-to" toml:"report-to"` + ReportURI string `csp:"report-uri" toml:"report-uri"` + RequireSRIFor []csp.RequireSRIFor `csp:"require-sri-for" toml:"require-sri-for"` + RequireTrustedTypesFor []csp.RequireTrustedTypesFor `csp:"require-trusted-types-for" toml:"require-trusted-types-for"` + Sandbox csp.Sandbox `csp:"sandbox" toml:"sandbox"` + ScriptSrc []string `csp:"script-src" toml:"script-src"` + ScriptSrcAttr []string `csp:"script-src-attr" toml:"script-src-attr"` + ScriptSrcElem []string `csp:"script-src-elem" toml:"script-src-elem"` + StyleSrc []string `csp:"style-src" toml:"style-src"` + StyleSrcAttr []string `csp:"style-src-attr" toml:"style-src-attr"` + StyleSrcElem []string `csp:"style-src-elem" toml:"style-src-elem"` + TrustedTypes []string `csp:"trusted-types" toml:"trusted-types"` + UpgradeInsecureRequests bool `csp:"upgrade-insecure-requests" toml:"upgrade-insecure-requests"` + WorkerSrc []string `csp:"worker-src" toml:"worker-src"` +} + +func (c *CSP) String() string { + return csp.Header(*c).String() +} |