-
Notifications
You must be signed in to change notification settings - Fork 474
/
etc_config.go
49 lines (43 loc) · 1.24 KB
/
etc_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
// Package etc_config implements config initialization of one spider.
package etc_config
import (
"github.com/hu17889/go_spider/core/common/config"
"github.com/hu17889/go_spider/core/common/util"
"os"
)
// Configpath gets default config path like "WD/etc/main.conf".
func configpath() string {
//wd, _ := os.Getwd()
wd := os.Getenv("GOPATH")
if wd == "" {
panic("GOPATH is not setted in env.")
}
logpath := wd + "/etc/"
filename := "main.conf"
err := os.MkdirAll(logpath, 0755)
if err != nil {
panic("logpath error : " + logpath + "\n")
}
return logpath + filename
}
// Config is a config singleton object for one spider.
var conf *config.Config
var path string
// StartConf is used in Spider for initialization at first time.
func StartConf(configFilePath string) *config.Config {
if configFilePath != "" && !util.IsFileExists(configFilePath) {
panic("config path is not valiad:" + configFilePath)
}
path = configFilePath
return Conf()
}
// Conf gets singleton instance of Config.
func Conf() *config.Config {
if conf == nil {
if path == "" {
path = configpath()
}
conf = config.NewConfig().Load(path)
}
return conf
}