Skip to content

Commit 8504b0a

Browse files
committed
Add config and corresponding tests
1 parent e21cc36 commit 8504b0a

2 files changed

Lines changed: 153 additions & 2 deletions

File tree

internal/config/config.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,45 @@
1-
// internal/config/config.go
1+
package config
2+
3+
import (
4+
"os"
5+
"time"
6+
7+
"github.com/Pavan-Rana/rate-limiter/internal/limiter"
8+
)
9+
10+
type Config struct {
11+
RedisAddr string
12+
GRPCAddr string
13+
HTTPAddr string
14+
defaultPolicy limiter.Policy
15+
policies map[string]limiter.Policy // per-API-key overrides
16+
}
17+
18+
func Load() (*Config, error) {
19+
return &Config{
20+
RedisAddr: getEnv("REDIS_ADDR", "localhost:6379"),
21+
GRPCAddr: getEnv("GRPC_ADDR", ":50051"),
22+
HTTPAddr: getEnv("HTTP_ADDR", ":8080"),
23+
defaultPolicy: limiter.Policy{
24+
Limit: 100,
25+
Window: time.Minute,
26+
},
27+
policies: make(map[string]limiter.Policy),
28+
}, nil
29+
}
30+
31+
func (c *Config) PolicyFor(apiKey string) (limiter.Policy, bool) {
32+
p, ok := c.policies[apiKey]
33+
return p, ok
34+
}
35+
36+
func (c *Config) GetDefault() limiter.Policy {
37+
return c.defaultPolicy
38+
}
39+
40+
func getEnv(key, fallback string) string {
41+
if v := os.Getenv(key); v != "" {
42+
return v
43+
}
44+
return fallback
45+
}

internal/config/config_test.go

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,108 @@
1-
// internal/config/config_test.go
1+
package config
2+
3+
import (
4+
"os"
5+
"testing"
6+
"time"
7+
8+
"github.com/Pavan-Rana/rate-limiter/internal/limiter"
9+
)
10+
11+
func TestLoad_Defaults(t *testing.T) {
12+
// Clear env to test fallbacks
13+
os.Unsetenv("REDIS_ADDR")
14+
os.Unsetenv("GRPC_ADDR")
15+
os.Unsetenv("HTTP_ADDR")
16+
17+
cfg, err := Load()
18+
if err != nil {
19+
t.Fatalf("expected no error, got %v", err)
20+
}
21+
22+
if cfg.RedisAddr != "localhost:6379" {
23+
t.Errorf("expected RedisAddr localhost:6379, got %s", cfg.RedisAddr)
24+
}
25+
26+
if cfg.GRPCAddr != ":50051" {
27+
t.Errorf("expected GRPCAddr :50051, got %s", cfg.GRPCAddr)
28+
}
29+
30+
if cfg.HTTPAddr != ":8080" {
31+
t.Errorf("expected HTTPAddr :8080, got %s", cfg.HTTPAddr)
32+
}
33+
}
34+
35+
func TestLoad_FromEnv(t *testing.T) {
36+
os.Setenv("REDIS_ADDR", "redis:1234")
37+
os.Setenv("GRPC_ADDR", ":9999")
38+
os.Setenv("HTTP_ADDR", ":7777")
39+
40+
defer os.Unsetenv("REDIS_ADDR")
41+
defer os.Unsetenv("GRPC_ADDR")
42+
defer os.Unsetenv("HTTP_ADDR")
43+
44+
cfg, err := Load()
45+
if err != nil {
46+
t.Fatalf("expected no error, got %v", err)
47+
}
48+
49+
if cfg.RedisAddr != "redis:1234" {
50+
t.Errorf("expected RedisAddr redis:1234, got %s", cfg.RedisAddr)
51+
}
52+
53+
if cfg.GRPCAddr != ":9999" {
54+
t.Errorf("expected GRPCAddr :9999, got %s", cfg.GRPCAddr)
55+
}
56+
57+
if cfg.HTTPAddr != ":7777" {
58+
t.Errorf("expected HTTPAddr :7777, got %s", cfg.HTTPAddr)
59+
}
60+
}
61+
62+
func TestGetDefaultPolicy(t *testing.T) {
63+
cfg, _ := Load()
64+
65+
p := cfg.GetDefault()
66+
67+
if p.Limit != 100 {
68+
t.Errorf("expected limit 100, got %d", p.Limit)
69+
}
70+
71+
if p.Window != time.Minute {
72+
t.Errorf("expected window 1 minute, got %v", p.Window)
73+
}
74+
}
75+
76+
func TestPolicyFor_NotFound(t *testing.T) {
77+
cfg, _ := Load()
78+
79+
_, ok := cfg.PolicyFor("nonexistent")
80+
81+
if ok {
82+
t.Errorf("expected policy to not exist")
83+
}
84+
}
85+
86+
func TestPolicyFor_Found(t *testing.T) {
87+
cfg, _ := Load()
88+
89+
testPolicy := limiter.Policy{
90+
Limit: 50,
91+
Window: time.Second * 30,
92+
}
93+
94+
cfg.policies["test-key"] = testPolicy
95+
96+
p, ok := cfg.PolicyFor("test-key")
97+
if !ok {
98+
t.Fatalf("expected policy to exist")
99+
}
100+
101+
if p.Limit != 50 {
102+
t.Errorf("expected limit 50, got %d", p.Limit)
103+
}
104+
105+
if p.Window != 30*time.Second {
106+
t.Errorf("expected window 30s, got %v", p.Window)
107+
}
108+
}

0 commit comments

Comments
 (0)