-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
59 lines (52 loc) · 1.54 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
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"encoding/json"
"io/ioutil"
)
type Config struct {
Namespace string `json:"namespace"`
Redis RedisBloomConfig `json:"redis"`
RabbitMq RabbitMqConfig `json:"rabbitMq"`
Elasticsearch ElasticsearchConfig `json:"elasticsearch"`
Seeds []string `json:"seeds"`
UserAgents []string `json:"userAgents"`
Workers int `json:"workers"`
Accepts []string `json:"accepts"`
// 优先级策略:0-9 表示优先级为常数,url-len 表示根据 URL 长度计算优先级(越短越优先),path-len 表示根据 URL 路径长度计算优先级(越短越优先)。
Priority string `json:"priority"`
Rules Rule `json:"rules"`
ResponseHeaders []string `json:"responseHeaders"`
}
type RedisBloomConfig struct {
Host string `json:"host"`
Auth string `json:"auth"`
}
type ElasticsearchConfig struct {
Address []string `json:"address"`
Username string `json:"username"`
Password string `json:"password"`
}
type RabbitMqConfig struct {
Url string `json:"url"`
Exchange string `json:"exchange"`
MaxLength int64 `json:"MaxLength"`
}
type Rule struct {
Allows []string `json:"allows"`
Forbid []string `json:"forbid"`
}
func loadConfig(path string) (*Config, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
cfg := &Config{}
err = json.Unmarshal(buf, &cfg)
if err != nil {
return nil, err
}
if cfg.Workers <= 0 {
cfg.Workers = 1
}
return cfg, nil
}