-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsetting.go
99 lines (94 loc) · 2.26 KB
/
setting.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package MagicSpider
import (
"fmt"
"github.com/hearecho/MagicSpider/utils"
"github.com/spf13/viper"
"os"
"reflect"
"time"
)
// Setting 配置
type Setting struct {
//爬虫名称
SpiderName string
//访问超时
TimeOut time.Duration
//runtime 路径
RuntimePath string
//MAxDepth 爬取最大深度
MaxDepth int
//爬取速率 1S爬取几次,默认速率1s 1w次(基本等于不限速)
Rate int
//返回文件类型 html,json 默认json
DocType string
//日志级别
LogLevel int
// 是否是分布式
Distribute bool
StaDB string
}
var S = &Setting{
SpiderName: "spider",
TimeOut: 3 * time.Second,
RuntimePath: "runtime/",
MaxDepth: 2,
Rate: 10000,
DocType: "html",
LogLevel: 1,
Distribute: false,
StaDB: "",
}
func printSettings() {
// 通过反射获取结构体中的值
t := reflect.TypeOf(*S)
for i := 0; i < t.NumField(); i++ {
name := t.Field(i).Name
v := reflect.ValueOf(*S)
val := v.FieldByName(name)
fmt.Printf("%s:%v\n", name, val)
}
}
func InitSetting() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("config/")
path, _ := os.Getwd()
viper.AddConfigPath(path)
err := viper.ReadInConfig()
if err != nil { // Handle errors reading the config file
utils.Error(fmt.Sprintf("Fatal error config file: will use default configuration \n", err))
printSettings()
return
}
if viper.IsSet("base.maxDepth") {
S.MaxDepth = viper.GetInt("base.maxDepth")
}
if viper.IsSet("base.spiderName") {
S.SpiderName = viper.GetString("base.spiderName")
}
if viper.IsSet("base.runtimePath") {
S.RuntimePath = viper.GetString("base.runtimePath")
}
if viper.IsSet("base.timout") {
S.TimeOut = viper.GetDuration("base.timout") * time.Second
}
if viper.IsSet("base.rate") {
S.Rate = viper.GetInt("base.rate")
}
if viper.IsSet("base.docType") {
S.DocType = viper.GetString("base.docType")
}
if viper.IsSet("base.logLevel") {
S.LogLevel = viper.GetInt("base.logLevel")
}
if viper.IsSet("base.distribute") {
// 只有在分布式环境下才会读取base.staDB
S.Distribute = viper.GetBool("base.distribute")
}
if viper.IsSet("base.staDB") {
S.StaDB = viper.GetString("base.staDB")
}
utils.Info("读取配置如下:")
printSettings()
}