stillbox/pkg/config/config.go

108 lines
2.9 KiB
Go
Raw Normal View History

2024-07-14 13:47:48 -04:00
package config
2024-07-14 17:39:03 -04:00
import (
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"
2024-07-14 17:39:03 -04:00
)
2024-11-24 08:40:14 -05:00
type Configuration struct {
Config
2024-11-24 10:38:19 -05:00
configPath *string `yaml:"-"`
2024-11-24 08:40:14 -05:00
}
2024-07-14 13:47:48 -04:00
type Config struct {
2025-01-20 20:28:25 -05:00
BaseURL jsontypes.URL `yaml:"baseURL"`
DumpRoutes bool `yaml:"dumpRoutes"`
DB DB `yaml:"db"`
CORS CORS `yaml:"cors"`
Auth Auth `yaml:"auth"`
Alerting Alerting `yaml:"alerting"`
Log []Logger `yaml:"log"`
Listen string `yaml:"listen"`
Public bool `yaml:"public"`
RateLimit RateLimit `yaml:"rateLimit"`
Notify Notify `yaml:"notify"`
Relay []Relay `yaml:"relay"`
2024-07-14 13:47:48 -04:00
}
2024-08-04 08:55:12 -04:00
type Auth struct {
2025-01-05 11:31:08 -05:00
JWTSecret string `yaml:"jwtsecret"`
AllowInsecure map[string]bool `yaml:"allowInsecureFor"`
SameSiteNoneWhenInsecure bool `yaml:"sameSiteNoneForInsecure"`
2024-08-04 08:55:12 -04:00
}
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 {
Connect string `yaml:"connect"`
LogQueries bool `yaml:"logQueries"`
Partition Partition `yaml:"partition"`
}
type Partition struct {
Enabled bool `yaml:"enabled"`
Schema string `yaml:"schema"`
Interval string `yaml:"interval"`
Retain int `yaml:"retain"`
PreProvision *int `yaml:"preProvision"`
Drop bool `yaml:"detach"`
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
}
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
}