2021-04-07 11:29:21 +02:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2021-04-11 11:08:27 +02:00
|
|
|
"encoding/json"
|
2021-04-09 18:00:40 +02:00
|
|
|
"errors"
|
2021-04-18 15:39:29 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-04-11 11:08:27 +02:00
|
|
|
"io/ioutil"
|
2021-04-08 12:16:33 +02:00
|
|
|
"strings"
|
2021-04-07 11:29:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2021-04-11 11:08:27 +02:00
|
|
|
Port int `json:"port"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
UtcOffset int `json:"utcOffset"`
|
|
|
|
DataPath string `json:"dataPath"`
|
2021-04-18 21:31:31 +02:00
|
|
|
ConString string `json:"conString"`
|
2021-04-11 11:08:27 +02:00
|
|
|
AllowedWebmentionSources []string `json:"allowedWebmentionSources"`
|
|
|
|
DisallowedWebmentionDomains []string `json:"disallowedWebmentionDomains"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) missingKeys() []string {
|
|
|
|
keys := []string{}
|
|
|
|
if c.Port == 0 {
|
|
|
|
keys = append(keys, "port")
|
|
|
|
}
|
|
|
|
if c.Token == "" {
|
|
|
|
keys = append(keys, "token")
|
|
|
|
}
|
2021-04-18 21:31:31 +02:00
|
|
|
if c.ConString == "" {
|
|
|
|
keys = append(keys, "conString")
|
|
|
|
}
|
2021-04-11 11:08:27 +02:00
|
|
|
if len(c.AllowedWebmentionSources) == 0 {
|
|
|
|
keys = append(keys, "allowedWebmentionSources")
|
|
|
|
}
|
|
|
|
return keys
|
2021-04-07 11:29:21 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 16:16:08 +02:00
|
|
|
func (c *Config) ContainsDisallowedDomain(url string) bool {
|
|
|
|
for _, domain := range c.DisallowedWebmentionDomains {
|
|
|
|
if strings.Contains(url, domain) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-09 18:00:40 +02:00
|
|
|
func (c *Config) IsAnAllowedDomain(url string) bool {
|
|
|
|
for _, domain := range c.AllowedWebmentionSources {
|
|
|
|
if domain == url {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-08 12:16:33 +02:00
|
|
|
func (c *Config) FetchDomain(url string) (string, error) {
|
|
|
|
for _, domain := range c.AllowedWebmentionSources {
|
2021-04-08 16:37:04 +02:00
|
|
|
if strings.Contains(url, domain) {
|
2021-04-08 12:16:33 +02:00
|
|
|
return domain, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", errors.New("no allowed domain found for url " + url)
|
|
|
|
}
|
|
|
|
|
2021-04-16 16:17:09 +02:00
|
|
|
func Configure() *Config {
|
2021-04-19 20:22:38 +02:00
|
|
|
c := config()
|
|
|
|
for _, domain := range c.AllowedWebmentionSources {
|
2021-04-08 12:16:33 +02:00
|
|
|
log.Info().Str("allowedDomain", domain).Msg("Configured")
|
|
|
|
}
|
2021-04-19 20:22:38 +02:00
|
|
|
return c
|
2021-04-08 12:16:33 +02:00
|
|
|
}
|
|
|
|
|
2021-04-16 16:17:09 +02:00
|
|
|
func config() *Config {
|
2021-04-11 11:08:27 +02:00
|
|
|
confData, err := ioutil.ReadFile("config.json")
|
2021-04-09 21:00:54 +02:00
|
|
|
if err != nil {
|
2021-04-11 11:08:27 +02:00
|
|
|
log.Warn().Msg("No config.json file found, reverting to defaults...")
|
|
|
|
return defaultConfig()
|
2021-04-07 11:29:21 +02:00
|
|
|
}
|
2021-04-11 11:08:27 +02:00
|
|
|
|
|
|
|
conf := &Config{}
|
|
|
|
err = json.Unmarshal(confData, conf)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Msg("config.json malformed JSON, reverting to defaults...")
|
|
|
|
return defaultConfig()
|
2021-04-07 11:29:21 +02:00
|
|
|
}
|
2021-04-11 11:08:27 +02:00
|
|
|
someMissingKeys := conf.missingKeys()
|
|
|
|
if len(someMissingKeys) > 0 {
|
|
|
|
log.Warn().Str("keys", strings.Join(someMissingKeys, ", ")).Msg("config.json is missing required keys, reverting to defaults...")
|
|
|
|
return defaultConfig()
|
|
|
|
}
|
|
|
|
return conf
|
|
|
|
}
|
2021-04-07 11:29:21 +02:00
|
|
|
|
2021-04-11 11:08:27 +02:00
|
|
|
func defaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
Port: 1337,
|
|
|
|
Token: "miauwkes",
|
2021-04-09 21:00:54 +02:00
|
|
|
UtcOffset: 60,
|
2021-04-18 21:31:31 +02:00
|
|
|
ConString: "data/mentions.db",
|
2021-04-09 21:00:54 +02:00
|
|
|
AllowedWebmentionSources: []string{"brainbaking.com", "jefklakscodex.com"},
|
|
|
|
DisallowedWebmentionDomains: []string{"youtube.com"},
|
2021-04-07 11:29:21 +02:00
|
|
|
}
|
|
|
|
}
|