/
getter.go
54 lines (47 loc) · 1.21 KB
/
getter.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
package app
import (
"errors"
"github.com/bh-qt/proxypool/log"
"github.com/bh-qt/proxypool/internal/cache"
"github.com/ghodss/yaml"
"github.com/bh-qt/proxypool/config"
"github.com/bh-qt/proxypool/pkg/getter"
)
var Getters = make([]getter.Getter, 0)
func InitConfigAndGetters(path string) (err error) {
err = config.Parse(path)
if err != nil {
return
}
if s := config.Config.SourceFiles; len(s) == 0 {
return errors.New("no sources")
} else {
initGetters(s)
}
return
}
func initGetters(sourceFiles []string) {
Getters = make([]getter.Getter, 0)
for _, path := range sourceFiles {
data, err := config.ReadFile(path)
if err != nil {
log.Errorln("Init SourceFile Error: %s\n", err.Error())
continue
}
sourceList := make([]config.Source, 0)
err = yaml.Unmarshal(data, &sourceList)
if err != nil {
log.Errorln("Init SourceFile Error: %s\n", err.Error())
continue
}
for _, source := range sourceList {
g, err := getter.NewGetter(source.Type, source.Options)
if err == nil && g != nil {
Getters = append(Getters, g)
log.Debugln("init getter: %s %v", source.Type, source.Options)
}
}
}
log.Infoln("Getter count: %d", len(Getters))
cache.GettersCount = len(Getters)
}