crawler_manager_go/crawler_manager.go
2017-04-14 17:19:02 +09:00

137 lines
1.8 KiB
Go

package crawler_manager
import (
"loafle.com/overflow/crawler_go/config"
"encoding/json"
"io/ioutil"
)
const (
address = "localhost:"
defaultPort = 50000
rootFolder = "/home/cm/"
ConfigFolder = rootFolder + "/config/"
BinaryFolder = rootFolder + "/container/"
PidFolder = rootFolder + "/pids/"
runFile = "tnc"
)
type crawler_manager struct {
}
var currentPort = defaultPort
var pidMap map[string]int
var portMap map[string]string
func init() {
pidMap = make(map[string]int)
portMap = make(map[string]string)
}
func ReadConfig(path string ) *config.Config {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
c := config.Config{}
json.Unmarshal(bytes, &c)
return &c
}
func InitContainer() {
cs := IsStartContainer()
var cpm map[string][]string = make(map[string][]string)
var ccl []string
for _, c := range cs {
ExistConfigFileDir(ConfigFolder, c, &ccl)
cpm[c] = ccl
RunContainer(&c, &cpm)
}
}
func IsStartContainer() []string {
files, _ := ioutil.ReadDir(ConfigFolder)
var cs []string
for _,file := range files {
if file.IsDir() {
b := ExistConfigFile(ConfigFolder, file.Name())
if b {
cs = append(cs, file.Name())
}
}
}
return cs
}
func ExistConfigFile(prePath string,dir string) bool {
files, _ := ioutil.ReadDir(prePath + "/" +dir)
for _,file := range files {
if file.IsDir() {
retB := ExistConfigFile(prePath + "/" + dir, file.Name())
if retB {
return true
}
} else {
return true
}
}
return false
}
func ExistConfigFileDir(prePath string,dir string, configCrawler *[]string) {
files, _ := ioutil.ReadDir(prePath + "/" +dir)
for _,file := range files {
if file.IsDir() {
ExistConfigFileDir(prePath + "/" + dir, file.Name(), configCrawler)
} else {
*configCrawler = append(*configCrawler, prePath + "/" +dir)
return;
}
}
}