commit 7faea797d00aa2af6ed96615b6346b87833e3d17 Author: huangsimin Date: Thu Jan 24 18:17:40 2019 +0800 初步结构 diff --git a/base.go b/base.go new file mode 100644 index 0000000..0461cb9 --- /dev/null +++ b/base.go @@ -0,0 +1,19 @@ +package simulator + +import "log" + +// must 处理err, 失败后panic +func must(err error) { + if err != nil { + panic(err) + } +} + +// notMust 处理err, 失败后返回 +func notMust(err error) bool { + if err != nil { + log.Fatalln(err) + return false + } + return true +} diff --git a/config.go b/config.go new file mode 100644 index 0000000..71a1702 --- /dev/null +++ b/config.go @@ -0,0 +1 @@ +package simulator diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..0615617 --- /dev/null +++ b/config_test.go @@ -0,0 +1,85 @@ +package simulator + +import ( + "strings" + + "gopkg.in/yaml.v2" +) + +// ADInfo ad 的一些属性,基础信息等 +type ADInfo struct { + Priority int `yaml:"priority"` + Device string `yaml:"device"` + Platform string `yaml:"platform"` + AreaCC string `yaml:"area_cc"` + Channel int `yaml:"channel"` + Media int `yaml:"media"` + SpiderID int `yaml:"spider_id"` + CatchAccountID int `yaml:"catch_account_id"` +} + +type CurlInfo struct { + TakeType int + Address string + URI string +} + +type ProxySetting struct { + Mode int `yaml:"mode"` + Proxies []string `yaml:"proxies"` +} + +type SpiderSetting struct { + // Mode int `yaml:"mode"` + // Proxies *YamlProxies `yaml:"proxies"` + Retry int `yaml:"retry"` + Crontab string `yaml:"crontab"` +} + +type Config struct { + ProxySetting `yaml:",inline"` + SpiderSetting `yaml:",inline"` + + ADInfo `yaml:",inline"` +} + +// NewConfig 加载并返回Config +func NewConfig(p string) *Config { + // f, err := os.Open(p) + // defer f.Close() + // if err != nil { + // panic(err) + // } + + f := strings.NewReader(p) + conf := &Config{ + + ProxySetting: ProxySetting{ + Mode: 0, + Proxies: []string{}, + }, + + SpiderSetting: SpiderSetting{ + Retry: 1, + Crontab: "", + }, + + ADInfo: ADInfo{ + Device: "", + Platform: "", + AreaCC: "", + Channel: -1, + Media: -1, + SpiderID: -1, + CatchAccountID: -1, + }, + } + + err := yaml.NewDecoder(f).Decode(conf) + + if err != nil { + panic(err) + } + + return conf +} diff --git a/task.go b/task.go new file mode 100644 index 0000000..71a1702 --- /dev/null +++ b/task.go @@ -0,0 +1 @@ +package simulator diff --git a/task_test.go b/task_test.go new file mode 100644 index 0000000..71a1702 --- /dev/null +++ b/task_test.go @@ -0,0 +1 @@ +package simulator