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",
"port" : "6379",
"ssl" : false,
"type" : "tcp"
"portType" : "tcp"
},
"auth" : {

View File

@ -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
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
}

View File

@ -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) {
}