/
init.go
51 lines (43 loc) · 1.01 KB
/
init.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
package proxypool
import (
"encoding/json"
"io/ioutil"
"strconv"
"github.com/Agzdjy/proxy-pool/spider"
"github.com/Agzdjy/proxy-pool/storage"
"github.com/go-redis/redis"
)
var ip181Spider = &spider.Ip181{}
func readJson(configPath string) map[string]string {
jsonData := map[string]string{}
bytes, err := ioutil.ReadFile(configPath)
if err != nil {
panic("config is undefined")
}
err = json.Unmarshal(bytes, &jsonData)
if err != nil {
panic("json format error")
}
return jsonData
}
func initStorage(configPath string) storage.Storage {
config := readJson(configPath)
store := config["store"]
var stor storage.Storage
switch store {
case "redis":
db, _ := strconv.Atoi(config["db"])
addr := config["address"]
password := config["password"]
stor = storage.NewRedisClient(&redis.Options{
Addr: addr,
Password: password,
DB: db,
})
}
return stor
}
func initSpider(stor storage.Storage) {
var ip181 spider.Spider = &spider.Ip181{}
ip181.Do("http://www.ip181.com/", stor)
}