2024-07-14 13:47:48 -04:00
|
|
|
package config
|
|
|
|
|
2024-07-14 17:39:03 -04:00
|
|
|
import (
|
2024-08-04 08:55:12 -04:00
|
|
|
"os"
|
2024-10-18 15:21:42 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
2024-08-04 08:55:12 -04:00
|
|
|
|
2024-11-13 09:24:11 -05:00
|
|
|
"dynatron.me/x/stillbox/internal/jsontypes"
|
2024-10-30 09:49:45 -04:00
|
|
|
|
2024-07-23 20:27:19 -04:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"gopkg.in/yaml.v3"
|
2024-07-14 17:39:03 -04:00
|
|
|
)
|
|
|
|
|
2024-07-14 13:47:48 -04:00
|
|
|
type Config struct {
|
2024-10-18 15:21:42 -04:00
|
|
|
DB DB `yaml:"db"`
|
|
|
|
CORS CORS `yaml:"cors"`
|
|
|
|
Auth Auth `yaml:"auth"`
|
2024-10-25 15:34:59 -04:00
|
|
|
Alerting Alerting `yaml:"alerting"`
|
2024-10-18 15:21:42 -04:00
|
|
|
Log []Logger `yaml:"log"`
|
|
|
|
Listen string `yaml:"listen"`
|
|
|
|
Public bool `yaml:"public"`
|
|
|
|
RateLimit RateLimit `yaml:"rateLimit"`
|
2024-10-31 00:10:53 -04:00
|
|
|
Notify Notify `yaml:"notify"`
|
2024-11-18 14:27:12 -05:00
|
|
|
Relay []Relay `yaml:"relay"`
|
2024-07-23 20:27:19 -04:00
|
|
|
|
|
|
|
configPath string
|
2024-07-14 13:47:48 -04:00
|
|
|
}
|
|
|
|
|
2024-08-04 08:55:12 -04:00
|
|
|
type Auth struct {
|
|
|
|
JWTSecret string `yaml:"jwtsecret"`
|
|
|
|
Domain string `yaml:"domain"`
|
|
|
|
AllowInsecure map[string]bool `yaml:"allowInsecureFor"`
|
|
|
|
}
|
|
|
|
|
2024-08-14 19:12:20 -04:00
|
|
|
type CORS struct {
|
|
|
|
AllowedOrigins []string `yaml:"allowedOrigins"`
|
|
|
|
}
|
|
|
|
|
2024-07-14 17:39:03 -04:00
|
|
|
type DB struct {
|
2024-11-15 10:37:58 -05:00
|
|
|
Connect string `yaml:"connect"`
|
|
|
|
LogQueries bool `yaml:"logQueries"`
|
2024-07-14 17:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-10-17 10:00:23 -04:00
|
|
|
type Logger struct {
|
2024-10-17 11:28:43 -04:00
|
|
|
File *string `yaml:"file"`
|
|
|
|
Level *string `yaml:"level"`
|
2024-10-17 10:00:23 -04:00
|
|
|
}
|
|
|
|
|
2024-10-18 15:21:42 -04:00
|
|
|
type RateLimit struct {
|
|
|
|
Enable bool `yaml:"enable"`
|
|
|
|
Requests int `yaml:"requests"`
|
|
|
|
Over time.Duration `yaml:"over"`
|
|
|
|
|
|
|
|
verifyError sync.Once
|
|
|
|
}
|
|
|
|
|
2024-10-25 15:34:59 -04:00
|
|
|
type Alerting struct {
|
2024-11-13 09:24:11 -05:00
|
|
|
Enable bool `yaml:"enable" form:"enable"`
|
|
|
|
LookbackDays uint `yaml:"lookbackDays" form:"lookbackDays"`
|
|
|
|
HalfLife jsontypes.Duration `yaml:"halfLife" form:"halfLife"`
|
|
|
|
Recent jsontypes.Duration `yaml:"recent" form:"recent"`
|
|
|
|
AlertThreshold float64 `yaml:"alertThreshold" form:"alertThreshold"`
|
|
|
|
Renotify *jsontypes.Duration `yaml:"renotify,omitempty" form:"renotify,omitempty"`
|
2024-10-31 00:10:53 -04:00
|
|
|
}
|
|
|
|
|
2024-11-18 14:27:12 -05:00
|
|
|
type Relay struct {
|
|
|
|
URL string `yaml:"url"`
|
|
|
|
APIKey string `yaml:"apiKey"`
|
|
|
|
Required bool `yaml:"required"`
|
|
|
|
}
|
|
|
|
|
2024-10-31 00:10:53 -04:00
|
|
|
type Notify []NotifyService
|
|
|
|
|
|
|
|
type NotifyService struct {
|
2024-11-07 23:17:16 -05:00
|
|
|
Provider string `yaml:"provider" json:"provider"`
|
|
|
|
SubjectTemplate *string `yaml:"subjectTemplate" json:"subjectTemplate"`
|
|
|
|
BodyTemplate *string `yaml:"bodyTemplate" json:"bodyTemplate"`
|
|
|
|
Config map[string]interface{} `yaml:"config" json:"config"`
|
2024-10-31 00:10:53 -04:00
|
|
|
}
|
|
|
|
|
2024-10-18 15:21:42 -04:00
|
|
|
func (rl *RateLimit) Verify() bool {
|
|
|
|
if rl.Enable {
|
|
|
|
if rl.Requests > 0 && rl.Over > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
rl.verifyError.Do(func() {
|
|
|
|
log.Error().Int("requests", rl.Requests).Str("over", rl.Over.String()).Msg("rate limit config makes no sense, disabled")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-10-17 11:28:43 -04:00
|
|
|
func (c *Config) PreRunE() func(*cobra.Command, []string) error {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
return c.ReadConfig()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(rootCommand *cobra.Command) *Config {
|
2024-07-23 20:27:19 -04:00
|
|
|
c := &Config{}
|
2024-10-17 11:28:43 -04:00
|
|
|
|
|
|
|
rootCommand.PersistentFlags().StringVarP(&c.configPath, "config", "c", "config.yaml", "configuration file")
|
|
|
|
|
2024-07-23 20:27:19 -04:00
|
|
|
return c
|
2024-07-14 17:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 20:27:19 -04:00
|
|
|
func (c *Config) ReadConfig() error {
|
|
|
|
cfgBytes, err := os.ReadFile(c.configPath)
|
2024-07-14 17:39:03 -04:00
|
|
|
if err != nil {
|
2024-07-23 20:27:19 -04:00
|
|
|
return err
|
2024-07-14 17:39:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(cfgBytes, c)
|
|
|
|
if err != nil {
|
2024-07-23 20:27:19 -04:00
|
|
|
return err
|
2024-07-14 17:39:03 -04:00
|
|
|
}
|
|
|
|
|
2024-11-03 07:19:03 -05:00
|
|
|
log.Info().Str("configPath", c.configPath).Msg("read config")
|
2024-07-23 20:27:19 -04:00
|
|
|
|
|
|
|
return nil
|
2024-07-14 13:47:48 -04:00
|
|
|
}
|