From 5e6410451cf672dcc4fb817265853a4276bf08de Mon Sep 17 00:00:00 2001 From: "jackdaw@loafle.com" Date: Thu, 13 Apr 2017 15:36:28 +0900 Subject: [PATCH] example config --- config/example.json | 2 +- crawler.go | 67 +++++++++++++++++++++++++++++++++------------ crawler_test.go | 49 ++++++++++++++++++--------------- 3 files changed, 77 insertions(+), 41 deletions(-) diff --git a/config/example.json b/config/example.json index 79426a8..3cf0a7c 100644 --- a/config/example.json +++ b/config/example.json @@ -5,7 +5,7 @@ "ip" : "192.168.1.104", "port" : "6379", "ssl" : false, - "type" : "tcp" + "portType" : "tcp" }, "auth" : { diff --git a/crawler.go b/crawler.go index 9ced5a6..8a03e90 100644 --- a/crawler.go +++ b/crawler.go @@ -1,48 +1,79 @@ package crawler -import "errors" +import ( + "encoding/json" + "errors" + "io/ioutil" + "log" +) type Internal interface { Internal(params map[string]interface{}) ([]byte, error) } type Crawler interface { - Init(path string) ([]byte,error) - Add(id string) ([]byte,error) - Remove(id string) ([]byte,error) - Get(id string) ([]byte,error) + Init(path string) ([]byte, error) + Add(id string) ([]byte, error) + Remove(id string) ([]byte, error) + Get(id string) ([]byte, error) } type CrawlerImpl struct { Crawler - configs map[string]interface{} - internal Internal + configs map[string]interface{} + internal Internal + configPath string } -func (c *CrawlerImpl) Init(path string) ([]byte,error) { +func (c *CrawlerImpl) Init(path string) ([]byte, error) { if c.configs == nil { c.configs = make(map[string]interface{}, 0) } - // load all file in path - return nil,nil -} -func (c *CrawlerImpl) Add(id string) ([]byte,error) { + c.configPath = path + files, err := ioutil.ReadDir(c.configPath) + if err != nil { + log.Panic(err) + } - //file load , input config - return nil,nil + for _, file := range files { + if file.IsDir() == true { + continue + } + _,err := c.Add(file.Name()) + if err != nil { + log.Panic(err) + } + } + + // load all file in path + return json.Marshal(true) } -func (c *CrawlerImpl) Remove(id string) ([]byte,error) { +func (c *CrawlerImpl) Add(id string) ([]byte, error) { + + b, err := ioutil.ReadFile(c.configPath + id) + if err != nil { + log.Panic(err) + } + + var m = make(map[string]interface{}, 0) + json.Unmarshal(b, &m) + c.configs[id] = m + + return json.Marshal(true) +} +func (c *CrawlerImpl) Remove(id string) ([]byte, error) { //remove in config - return nil,nil + delete(c.configs,id) + return json.Marshal(true) } -func (c *CrawlerImpl) Get(id string)([]byte,error) { +func (c *CrawlerImpl) Get(id string) ([]byte, error) { if c.internal != nil { out, err := c.internal.Internal(c.GetConfig(id)) if err != nil { //set error fail, message - return nil,err + return nil, err } return out, nil } diff --git a/crawler_test.go b/crawler_test.go index 30e4be5..9ccd392 100644 --- a/crawler_test.go +++ b/crawler_test.go @@ -2,30 +2,35 @@ package crawler import ( "testing" + "encoding/json" + "github.com/stretchr/testify/assert" ) -func TestCrawler(t *testing.T) { +func initConfig() CrawlerImpl{ + c := CrawlerImpl{} + c.Init("/root/gowork/src/loafle.com/overflow/crawler_go/config/") + return c +} - //r := NewRedisHeahthCrawler() - // - //// test config - // - //m := make(map[string]interface{}, 0) - // - //r.configs = make(map[string]interface{}, 0) - //m["ip"] = "192.168.1.104" - //m["port"] = "6379" - //m["portType"] = "tcp" - //m["ssl"] = false - // - //r.configs["redis"] = m - // - //out := param.Output{} - //r.Get("redis", &out) - // - //var check bool - //json.Unmarshal(out.Data, &check) - // - //assert.Equal(t, true, check) + +func TestCrawlerInit(t *testing.T) { + + c := initConfig() + m := c.configs["example.json"].(map[string]interface{}) + assert.Equal(t, m["id"].(string) , "SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734") + +} + +func TestCrawlerAdd(t *testing.T) { } + +func TestCrawlerRemove(t *testing.T) { + +} + +func TestCrawlerGet(t *testing.T) { + +} + +