Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
Allow env expansion in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmeister committed Dec 8, 2021
1 parent c455f13 commit 997e939
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
8 changes: 4 additions & 4 deletions example.config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
redis:
address: 127.0.0.1:6379
address: ${REDIS_HOST:-127.0.0.1}:${REDIS_PORT:number:-6379}
limits:
interval: 10
anonymous: 10
identified: 60
interval: ${LIMIT_INTERVAL:number:-10}
anonymous: ${LIMIT_ANONYMOUS:number:-10}
identified: ${LIMIT_IDENTIFIED:number:-60}
other:
system: 0
identifiers:
Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@ module github.com/airfocusio/rate-limit-proxy
go 1.17

require (
github.com/airfocusio/go-expandenv v0.1.0-alpha.2
github.com/go-redis/redis/v8 v8.11.4
github.com/golang-jwt/jwt/v4 v4.2.0
github.com/google/go-cmp v0.5.6
github.com/stretchr/testify v1.5.1
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
6 changes: 5 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/airfocusio/go-expandenv v0.1.0-alpha.2 h1:by1LmIeCHPlVVN1gb0OoP6FnDmQGDCdtv+lGjKBHWk4=
github.com/airfocusio/go-expandenv v0.1.0-alpha.2/go.mod h1:UB5QYHJ7klVI34hnK9G3xtveYnKUAIC94hpP9rvzl2o=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -50,8 +52,9 @@ github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAl
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -113,5 +116,6 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17 changes: 16 additions & 1 deletion internal/proxy-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"fmt"

"github.com/airfocusio/go-expandenv"
"github.com/google/go-cmp/cmp"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -38,7 +39,21 @@ type RateLimitProxyConfig struct {
}

// LoadRateLimitProxyConfig ...
func LoadRateLimitProxyConfig(bytes []byte) (*RateLimitProxyConfig, *[]Identifier, error) {
func LoadRateLimitProxyConfig(bytesRaw []byte) (*RateLimitProxyConfig, *[]Identifier, error) {
var expansionTemp interface{}
err := yaml.Unmarshal(bytesRaw, &expansionTemp)
if err != nil {
return nil, nil, err
}
expansionTemp, err = expandenv.ExpandEnv(expansionTemp)
if err != nil {
return nil, nil, err
}
bytes, err := yaml.Marshal(expansionTemp)
if err != nil {
return nil, nil, err
}

config := &RateLimitProxyConfig{}
if err := yaml.Unmarshal(bytes, config); err != nil {
return nil, nil, err
Expand Down

0 comments on commit 997e939

Please sign in to comment.