Organize backend into product executable and shared Go modules
This commit is contained in:
1 parent
981e84a4b4
commit
f0720ec66b
382 files changed
+1944
-6989
No files matched your search
@@ -0,0 +1,6 @@
|
||||
package config
|
||||
|
||||
type Config struct {
|
||||
AllowedPurposes []string `yaml:"allowed_purposes"`
|
||||
RateLimits *VerificationRateLimitsConfig `yaml:"rate_limits"`
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
sharedlimit "git.sendico.io/tech/sxchnge/shared/ratelimit"
|
||||
)
|
||||
|
||||
type VerificationRateLimitsConfig struct {
|
||||
PerDestination sharedlimit.WindowConfig `yaml:"per_destination"`
|
||||
PerIP sharedlimit.WindowConfig `yaml:"per_ip"`
|
||||
}
|
||||
|
||||
type VerificationRateLimits struct {
|
||||
PerDestination sharedlimit.Window
|
||||
PerIP sharedlimit.Window
|
||||
}
|
||||
|
||||
func DefaultVerificationRateLimits() VerificationRateLimits {
|
||||
return VerificationRateLimits{
|
||||
PerDestination: sharedlimit.Window{Limit: 5, Window: time.Hour},
|
||||
PerIP: sharedlimit.Window{Limit: 20, Window: time.Hour},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) ResolveRateLimits() (VerificationRateLimits, error) {
|
||||
if c == nil || c.RateLimits == nil {
|
||||
return DefaultVerificationRateLimits(), nil
|
||||
}
|
||||
destination, err := sharedlimit.ResolveWindow(c.RateLimits.PerDestination, "verification.rate_limits.per_destination")
|
||||
if err != nil {
|
||||
return VerificationRateLimits{}, err
|
||||
}
|
||||
ip, err := sharedlimit.ResolveWindow(c.RateLimits.PerIP, "verification.rate_limits.per_ip")
|
||||
if err != nil {
|
||||
return VerificationRateLimits{}, err
|
||||
}
|
||||
return VerificationRateLimits{PerDestination: destination, PerIP: ip}, nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.sendico.io/tech/sxchnge/shared/errors"
|
||||
sharedlimit "git.sendico.io/tech/sxchnge/shared/ratelimit"
|
||||
)
|
||||
|
||||
func TestVerificationRateLimitsDefaults(t *testing.T) {
|
||||
got, err := (*Config)(nil).ResolveRateLimits()
|
||||
if err != nil {
|
||||
t.Fatalf("resolve defaults: %v", err)
|
||||
}
|
||||
if got.PerDestination.Limit != 5 || got.PerDestination.Window != time.Hour ||
|
||||
got.PerIP.Limit != 20 || got.PerIP.Window != time.Hour {
|
||||
t.Fatalf("unexpected defaults: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerificationRateLimitsRejectInvalidPolicy(t *testing.T) {
|
||||
cfg := &Config{RateLimits: &VerificationRateLimitsConfig{
|
||||
PerDestination: sharedlimit.WindowConfig{Limit: 5, Window: "1h"},
|
||||
PerIP: sharedlimit.WindowConfig{Limit: 0, Window: "1h"},
|
||||
}}
|
||||
_, err := cfg.ResolveRateLimits()
|
||||
if !errors.Is(err, merrors.ErrInvalidArg) {
|
||||
t.Fatalf("expected invalid argument, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user