example config

This commit is contained in:
jackdaw@loafle.com 2017-04-13 15:36:28 +09:00
parent 3e38a7cf3c
commit 5e6410451c
3 changed files with 77 additions and 41 deletions

View File

@ -5,7 +5,7 @@
"ip" : "192.168.1.104", "ip" : "192.168.1.104",
"port" : "6379", "port" : "6379",
"ssl" : false, "ssl" : false,
"type" : "tcp" "portType" : "tcp"
}, },
"auth" : { "auth" : {

View File

@ -1,48 +1,79 @@
package crawler package crawler
import "errors" import (
"encoding/json"
"errors"
"io/ioutil"
"log"
)
type Internal interface { type Internal interface {
Internal(params map[string]interface{}) ([]byte, error) Internal(params map[string]interface{}) ([]byte, error)
} }
type Crawler interface { type Crawler interface {
Init(path string) ([]byte,error) Init(path string) ([]byte, error)
Add(id string) ([]byte,error) Add(id string) ([]byte, error)
Remove(id string) ([]byte,error) Remove(id string) ([]byte, error)
Get(id string) ([]byte,error) Get(id string) ([]byte, error)
} }
type CrawlerImpl struct { type CrawlerImpl struct {
Crawler Crawler
configs map[string]interface{} configs map[string]interface{}
internal Internal internal Internal
configPath string
} }
func (c *CrawlerImpl) Init(path string) ([]byte,error) { func (c *CrawlerImpl) Init(path string) ([]byte, error) {
if c.configs == nil { if c.configs == nil {
c.configs = make(map[string]interface{}, 0) c.configs = make(map[string]interface{}, 0)
} }
// load all file in path c.configPath = path
return nil,nil files, err := ioutil.ReadDir(c.configPath)
} if err != nil {
func (c *CrawlerImpl) Add(id string) ([]byte,error) { log.Panic(err)
}
//file load , input config for _, file := range files {
return nil,nil 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 //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 { if c.internal != nil {
out, err := c.internal.Internal(c.GetConfig(id)) out, err := c.internal.Internal(c.GetConfig(id))
if err != nil { if err != nil {
//set error fail, message //set error fail, message
return nil,err return nil, err
} }
return out, nil return out, nil
} }

View File

@ -2,30 +2,35 @@ package crawler
import ( import (
"testing" "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()
// func TestCrawlerInit(t *testing.T) {
//// test config
// c := initConfig()
//m := make(map[string]interface{}, 0) m := c.configs["example.json"].(map[string]interface{})
// assert.Equal(t, m["id"].(string) , "SOEJWEOJWOEJOSDJFOASDJFOSDFO2903870928734")
//r.configs = make(map[string]interface{}, 0)
//m["ip"] = "192.168.1.104" }
//m["port"] = "6379"
//m["portType"] = "tcp" func TestCrawlerAdd(t *testing.T) {
//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 TestCrawlerRemove(t *testing.T) {
}
func TestCrawlerGet(t *testing.T) {
}