generated from xiachufang/go-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconfig.go
46 lines (40 loc) · 1.11 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package ipfilter
import (
"encoding/json"
"fmt"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
)
// Config is config of ipfilter
type Config struct {
Deny []string `json:"deny"`
Allow []string `json:"allow"`
// header keys to parse real ip, default to []string{X-Forwarded-For, X-Real-Ip}
IPHeaders []string `json:"ip_headers"`
}
var (
Namespace = "github_com/xiachufang/krakend-ipfilter"
// default parse real ip from `X-Forwarded-For` or `X-Real-Ip`
defaultIPHeaders = []string{"X-Forwarded-For", "X-Real-Ip"}
)
// ParseConfig build ip filter's Config
func ParseConfig(e config.ExtraConfig, logger logging.Logger) *Config {
v, ok := e[Namespace].(map[string]interface{})
if !ok {
return nil
}
data, err := json.Marshal(v)
if err != nil {
logger.Error(fmt.Sprintf("marshal krakend-ipfilter config error: %v", err))
return nil
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
logger.Error(fmt.Sprintf("unmarshal krakend-ipfilter config error: %v", err))
return nil
}
if len(cfg.IPHeaders) == 0 {
cfg.IPHeaders = defaultIPHeaders
}
return &cfg
}