diff --git a/.gitmodules b/.gitmodules index ffd5711..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +0,0 @@ -[submodule "webroot"] - path = webroot - url = https://github.com/monitormap/micro-web.git - branch = gh-pages diff --git a/config_example.yaml b/config_example.yaml new file mode 100644 index 0000000..0a540ec --- /dev/null +++ b/config_example.yaml @@ -0,0 +1,17 @@ +--- +responedd: + enable: true + collectinterval: 15 +webserver: + enable: false + port: 8080 + address: 127.0.0.1 + webroot: webroot + websocketnode: false +nodes: + enable: true + nodes_path: /var/www/html/meshviewer/nodes.json + graphs_path: /var/www/html/meshviewer/graphs.json + saveinterval: 5 + aliases_enable: false + aliases_path: webroot/aliases.json diff --git a/main.go b/main.go index 993ae76..5542f31 100644 --- a/main.go +++ b/main.go @@ -18,68 +18,70 @@ import ( ) var ( - wsserverForNodes = websocketserver.NewServer("/nodes") - respondDaemon *respond.Daemon - nodes = models.NewNodes() - aliases = models.NewNodes() - listenAddr string - listenPort string - collectInterval time.Duration - httpDir string - outputNodesFile string - outputAliasesFile string - saveInterval time.Duration + configFile string + config *models.Config + wsserverForNodes *websocketserver.Server + respondDaemon *respond.Daemon + nodes = models.NewNodes() + aliases = models.NewNodes() ) func main() { - var collectSeconds, saveSeconds int - - flag.StringVar(&listenAddr, "host", "", "path aliases.json file") - flag.StringVar(&listenPort, "port", "8080", "path aliases.json file") - flag.IntVar(&collectSeconds, "collectInterval", 15, "interval for data collections") - flag.StringVar(&httpDir, "httpdir", "webroot", "a implemented static file webserver") - flag.StringVar(&outputNodesFile, "path-nodes", "webroot/nodes.json", "path nodes.json file") - flag.StringVar(&outputAliasesFile, "path-aliases", "webroot/aliases.json", "path aliases.json file") - flag.IntVar(&saveSeconds, "saveInterval", 60, "interval for data saving") + flag.StringVar(&configFile, "c", "config.yml", "path of configuration file (default:config.yaml)") flag.Parse() + config = models.ConfigReadFile(configFile) - collectInterval = time.Second * time.Duration(collectSeconds) - saveInterval = time.Second * time.Duration(saveSeconds) + collectInterval := time.Second * time.Duration(config.Responedd.CollectInterval) + saveInterval := time.Second * time.Duration(config.Nodes.SaveInterval) - go wsserverForNodes.Listen() - go nodes.Saver(outputNodesFile, saveInterval) - go aliases.Saver(outputAliasesFile, saveInterval) - respondDaemon = respond.NewDaemon(func(coll *respond.Collector, res *respond.Response) { + if config.Nodes.Enable { + go nodes.Saver(config.Nodes.NodesPath, saveInterval) + } + if config.Nodes.AliasesEnable { + go aliases.Saver(config.Nodes.AliasesPath, saveInterval) + } - switch coll.CollectType { - case "neighbours": - result := &data.NeighbourStruct{} - if json.Unmarshal(res.Raw, result) == nil { - node := nodes.Get(result.NodeId) - node.Neighbours = result - } - case "nodeinfo": - result := &data.NodeInfo{} - if json.Unmarshal(res.Raw, result) == nil { - node := nodes.Get(result.NodeId) - node.Nodeinfo = result - } - case "statistics": - result := &data.StatisticsStruct{} - if json.Unmarshal(res.Raw, result) == nil { - node := nodes.Get(result.NodeId) - node.Statistics = result - } - default: - log.Println("unknown CollectType:", coll.CollectType) + if config.Webserver.Enable { + if config.Webserver.WebsocketNode { + wsserverForNodes = websocketserver.NewServer("/nodes") + go wsserverForNodes.Listen() } - }) - go respondDaemon.ListenAndSend(collectInterval) + http.Handle("/", http.FileServer(http.Dir(config.Webserver.Webroot))) + } + + if config.Responedd.Enable { + respondDaemon = respond.NewDaemon(func(coll *respond.Collector, res *respond.Response) { + + switch coll.CollectType { + case "neighbours": + result := &data.NeighbourStruct{} + if json.Unmarshal(res.Raw, result) == nil { + node := nodes.Get(result.NodeId) + node.Neighbours = result + } + case "nodeinfo": + result := &data.NodeInfo{} + if json.Unmarshal(res.Raw, result) == nil { + node := nodes.Get(result.NodeId) + node.Nodeinfo = result + } + case "statistics": + result := &data.StatisticsStruct{} + if json.Unmarshal(res.Raw, result) == nil { + node := nodes.Get(result.NodeId) + node.Statistics = result + } + default: + log.Println("unknown CollectType:", coll.CollectType) + } + }) + go respondDaemon.ListenAndSend(collectInterval) + } - http.Handle("/", http.FileServer(http.Dir(httpDir))) //TODO bad - log.Fatal(http.ListenAndServe(net.JoinHostPort(listenAddr, listenPort), nil)) - + if config.Webserver.Enable { + log.Fatal(http.ListenAndServe(net.JoinHostPort(config.Webserver.Address, config.Webserver.Port), nil)) + } // Wait for End sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) diff --git a/models/config.go b/models/config.go new file mode 100644 index 0000000..ff61593 --- /dev/null +++ b/models/config.go @@ -0,0 +1,45 @@ +package models + +import ( + "io/ioutil" + "log" + + "gopkg.in/yaml.v2" +) + +//Config the config File of this daemon +type Config struct { + Responedd struct { + Enable bool `yaml:"enable"` + Port string `yaml:"port"` + Address string `yaml:"address"` + CollectInterval int `yaml:"collectinterval"` + } `yaml:"responedd"` + Webserver struct { + Enable bool `yaml:"enable"` + Port string `yaml:"port"` + Address string `yaml:"address"` + Webroot string `yaml:"webroot"` + WebsocketNode bool `yaml:"websocketnode"` + WebsocketAliases bool `yaml:"websocketaliases"` + } `yaml:"webserver"` + Nodes struct { + Enable bool `yaml:"enable"` + NodesPath string `yaml:"nodes_path"` + GraphsPath string `yaml:"graphs_path"` + AliasesEnable bool `yaml:"aliases_enable"` + AliasesPath string `yaml:"aliases_path"` + SaveInterval int `yaml:"saveinterval"` + } `yaml:"nodes"` +} + +//ConfigReadFile reads a Config models by path to a yml file +func ConfigReadFile(path string) *Config { + config := &Config{} + file, _ := ioutil.ReadFile(path) + err := yaml.Unmarshal(file, &config) + if err != nil { + log.Fatal(err) + } + return config +} diff --git a/webroot b/webroot deleted file mode 160000 index fb78d78..0000000 --- a/webroot +++ /dev/null @@ -1 +0,0 @@ -Subproject commit fb78d78bd3089cfdeeb191f5401b538de926197c