This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathgospider.go
179 lines (154 loc) · 3.59 KB
/
gospider.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// GoSpider is a simple go crawler framework.
// User only need to care about the rules of page, provides web page to manage task. Base on colly.
package gospider
import (
"log"
"os"
"strconv"
"strings"
"github.com/nange/gospider/web/core"
"github.com/nange/gospider/common"
"github.com/nange/gospider/web"
"github.com/pkg/errors"
)
const (
// Name is the name of gospider
Name = "gospider"
// Version is the version of gospider
Version = "1.0.0"
)
// GoSpider provides the spider instance for a scraping job
type GoSpider struct {
backend string
mysql common.MySQLConf
web *web.Server
}
// New return a new instance of GoSpider
func New(opts ...func(*GoSpider)) *GoSpider {
gs := &GoSpider{}
gs.init()
for _, f := range opts {
f(gs)
}
gs.parseSettingsFromEnv()
return gs
}
// Run start GoSpider server
func (gs *GoSpider) Run() error {
gs.print()
db, err := common.NewGormDB(gs.mysql)
if err != nil {
return errors.Wrap(err, "new gorm db failed")
}
core.SetGormDB(db)
return errors.Wrap(gs.web.Run(), "web run failed")
}
// BackendMySQL sets the gospider backend with mysql
func BackendMySQL() func(*GoSpider) {
return func(gs *GoSpider) {
gs.backend = "mysql"
}
}
// BackendSQLite sets the gospider backend with sqllite
func BackendSQLite() func(*GoSpider) {
return func(gs *GoSpider) {
gs.backend = "sqlite"
}
}
// MySQLHost sets the mysql host
func MySQLHost(host string) func(*GoSpider) {
return func(gs *GoSpider) {
gs.mysql.Host = host
}
}
// MySQLPort sets the mysql port
func MySQLPort(port int) func(*GoSpider) {
return func(gs *GoSpider) {
gs.mysql.Port = port
}
}
// MySQLUser sets the mysql user
func MySQLUser(user string) func(*GoSpider) {
return func(gs *GoSpider) {
gs.mysql.User = user
}
}
// MySQLPassword sets the mysql password
func MySQLPassword(password string) func(*GoSpider) {
return func(gs *GoSpider) {
gs.mysql.Password = password
}
}
// MySQLDBName sets the mysql dbname
func MySQLDBName(dbname string) func(*GoSpider) {
return func(gs *GoSpider) {
gs.mysql.DBName = dbname
}
}
// WebIP sets the bind IP of gospider server
func WebIP(ip string) func(*GoSpider) {
return func(gs *GoSpider) {
gs.web.IP = ip
}
}
// WebPort sets the bind Port of gospider server
func WebPort(port int) func(*GoSpider) {
return func(gs *GoSpider) {
gs.web.Port = port
}
}
func (gs *GoSpider) init() {
gs.backend = "mysql"
gs.mysql.Host = "127.0.0.1"
gs.mysql.Port = 3306
gs.mysql.User = "root"
gs.mysql.MaxIdleConns = 3
gs.mysql.MaxOpenConns = 10
gs.web = &web.Server{Port: 8080}
}
func (gs *GoSpider) print() {
log.Println(Name, Version)
log.Printf("gospider backend conf:%+v\n", gs.mysql)
}
var envMap = map[string]func(*GoSpider, string){
"DB_HOST": func(gs *GoSpider, val string) {
gs.mysql.Host = val
},
"DB_PORT": func(gs *GoSpider, val string) {
port, err := strconv.Atoi(val)
if err == nil {
gs.mysql.Port = port
}
},
"DB_USER": func(gs *GoSpider, val string) {
gs.mysql.User = val
},
"DB_PASSWORD": func(gs *GoSpider, val string) {
gs.mysql.Password = val
},
"DB_NAME": func(gs *GoSpider, val string) {
gs.mysql.DBName = val
},
"WEB_IP": func(gs *GoSpider, val string) {
gs.web.IP = val
},
"WEB_PORT": func(gs *GoSpider, val string) {
port, err := strconv.Atoi(val)
if err == nil {
gs.web.Port = port
}
},
}
func (gs *GoSpider) parseSettingsFromEnv() {
for _, e := range os.Environ() {
if !strings.HasPrefix(e, "GOSPIDER_") {
continue
}
pair := strings.SplitN(e[9:], "=", 2)
if f, ok := envMap[pair[0]]; ok {
f(gs, pair[1])
} else {
log.Println("Unknown env variable:", pair[0])
}
}
}