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

72 lines
1.3 KiB
Go

package internal
import (
"errors"
"fmt"
"math/rand"
"net/url"
"strings"
"time"
)
var (
ErrConfigPathMissing = errors.New("error: config file missing")
)
// 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 }
// 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
}