88 lines
2.2 KiB
Go
88 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
|
|
cda "git.loafle.net/commons/di-go/annotation"
|
|
cdr "git.loafle.net/commons/di-go/registry"
|
|
logging "git.loafle.net/commons/logging-go"
|
|
"git.loafle.net/overflow/crawler-go"
|
|
|
|
// For annotation
|
|
_ "git.loafle.net/overflow/commons-go/core/annotation"
|
|
)
|
|
|
|
var CrawlerServiceType = reflect.TypeOf((*CrawlerService)(nil))
|
|
|
|
func init() {
|
|
cdr.RegisterType(CrawlerServiceType)
|
|
}
|
|
|
|
type CrawlerService struct {
|
|
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
|
|
|
|
SensorConfigService *SensorConfigService `annotation:"@Inject()"`
|
|
Crawlers map[string]crawler.Crawler `annotation:"@Resource(name='CONTAINER_CRAWLERS')"`
|
|
}
|
|
|
|
func (s *CrawlerService) InitService() error {
|
|
if nil == s.Crawlers {
|
|
return fmt.Errorf("Crawlers is not set")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *CrawlerService) StartService() error {
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *CrawlerService) StopService() {
|
|
|
|
}
|
|
|
|
func (s *CrawlerService) DestroyService() {
|
|
|
|
}
|
|
|
|
func (s *CrawlerService) Auth(crawlerName string, auth map[string]interface{}) error {
|
|
_crawler, ok := s.Crawlers[crawlerName]
|
|
if !ok {
|
|
return fmt.Errorf("There is no crawler[%s]", crawlerName)
|
|
}
|
|
|
|
logging.Logger().Debugf("Auth invoked with [%v]", auth)
|
|
|
|
err := _crawler.Auth(auth)
|
|
if nil != err {
|
|
return fmt.Errorf("Failed to authentication from crawler[%s] %v", crawlerName, err)
|
|
}
|
|
logging.Logger().Debugf("Auth success with[%v]", auth)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *CrawlerService) Get(sensorConfigID string) (map[string]string, error) {
|
|
sensorConfig, ok := s.SensorConfigService.sensorConfigs[sensorConfigID]
|
|
if !ok {
|
|
return nil, fmt.Errorf("There is no sensor config for id[%s]", sensorConfigID)
|
|
}
|
|
_crawlerName := sensorConfig.Crawler.MetaCrawlerKey
|
|
_crawler, ok := s.Crawlers[_crawlerName]
|
|
if !ok {
|
|
return nil, fmt.Errorf("There is no crawler[%s] for id[%s]", _crawlerName, sensorConfigID)
|
|
}
|
|
|
|
logging.Logger().Debugf("Get invoked with sensor config[%s]", sensorConfigID)
|
|
|
|
result, err := _crawler.Get(sensorConfig)
|
|
if nil != err {
|
|
return nil, fmt.Errorf("Failed to get data from crawler[%s] for id[%s] %v", _crawlerName, sensorConfigID, err)
|
|
}
|
|
logging.Logger().Debugf("Get success config[%v]", result)
|
|
|
|
return result, nil
|
|
}
|