Files
spyda/internal/config.go
2022-10-05 11:11:40 +10:00

93 lines
1.9 KiB
Go

package internal
import (
"errors"
"fmt"
"math/rand"
"net/url"
"strings"
"time"
"github.com/gabstv/merger"
log "github.com/sirupsen/logrus"
)
var (
ErrConfigPathMissing = errors.New("error: config file missing")
)
// Settings contains Pod Settings that can be customised via the Web UI
type Settings struct {
Name string `yaml:"pod_name"`
Description string `yaml:"pod_description"`
}
// Config contains the server configuration parameters
type Config struct {
Debug bool
Data string
Name string
Description string
Store string
Theme string
BaseURL string
AdminName string
AdminEmail string
SearchPrompts []string
ResultsPerPage int
SessionExpiry time.Duration
CookieSecret string
SMTPHost string
SMTPPort int
SMTPUser string
SMTPPass string
SMTPFrom string
baseURL *url.URL
}
func (c *Config) IsLocalURL(url string) bool {
if NormalizeURL(url) == "" {
return false
}
return strings.HasPrefix(NormalizeURL(url), NormalizeURL(c.BaseURL))
}
func (c *Config) LocalURL() *url.URL { return c.baseURL }
// Settings returns a `Settings` struct containing pod settings that can
// then be persisted to disk to override some configuration options.
func (c *Config) Settings() *Settings {
settings := &Settings{}
if err := merger.MergeOverwrite(settings, c); err != nil {
log.WithError(err).Warn("error creating pod settings")
}
return settings
}
// RandomSearchPrompt returns a random Search Prompt for display by the UI
func (c *Config) RandomSearchPrompt() string {
n := rand.Int() % len(c.SearchPrompts)
return c.SearchPrompts[n]
}
// Validate validates the configuration is valid which for the most part
// just ensures that default secrets are actually configured correctly
func (c *Config) Validate() error {
if c.Debug {
return nil
}
if c.CookieSecret == InvalidConfigValue {
return fmt.Errorf("error: COOKIE_SECRET is not configured")
}
return nil
}