switch to config-file
This commit is contained in:
parent
646f8c5a9a
commit
5df9ee8f0e
4
.gitmodules
vendored
4
.gitmodules
vendored
@ -1,4 +0,0 @@
|
|||||||
[submodule "webroot"]
|
|
||||||
path = webroot
|
|
||||||
url = https://github.com/monitormap/micro-web.git
|
|
||||||
branch = gh-pages
|
|
17
config_example.yaml
Normal file
17
config_example.yaml
Normal file
@ -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
|
106
main.go
106
main.go
@ -18,68 +18,70 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
wsserverForNodes = websocketserver.NewServer("/nodes")
|
configFile string
|
||||||
respondDaemon *respond.Daemon
|
config *models.Config
|
||||||
nodes = models.NewNodes()
|
wsserverForNodes *websocketserver.Server
|
||||||
aliases = models.NewNodes()
|
respondDaemon *respond.Daemon
|
||||||
listenAddr string
|
nodes = models.NewNodes()
|
||||||
listenPort string
|
aliases = models.NewNodes()
|
||||||
collectInterval time.Duration
|
|
||||||
httpDir string
|
|
||||||
outputNodesFile string
|
|
||||||
outputAliasesFile string
|
|
||||||
saveInterval time.Duration
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var collectSeconds, saveSeconds int
|
flag.StringVar(&configFile, "c", "config.yml", "path of configuration file (default:config.yaml)")
|
||||||
|
|
||||||
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.Parse()
|
flag.Parse()
|
||||||
|
config = models.ConfigReadFile(configFile)
|
||||||
|
|
||||||
collectInterval = time.Second * time.Duration(collectSeconds)
|
collectInterval := time.Second * time.Duration(config.Responedd.CollectInterval)
|
||||||
saveInterval = time.Second * time.Duration(saveSeconds)
|
saveInterval := time.Second * time.Duration(config.Nodes.SaveInterval)
|
||||||
|
|
||||||
go wsserverForNodes.Listen()
|
if config.Nodes.Enable {
|
||||||
go nodes.Saver(outputNodesFile, saveInterval)
|
go nodes.Saver(config.Nodes.NodesPath, saveInterval)
|
||||||
go aliases.Saver(outputAliasesFile, saveInterval)
|
}
|
||||||
respondDaemon = respond.NewDaemon(func(coll *respond.Collector, res *respond.Response) {
|
if config.Nodes.AliasesEnable {
|
||||||
|
go aliases.Saver(config.Nodes.AliasesPath, saveInterval)
|
||||||
|
}
|
||||||
|
|
||||||
switch coll.CollectType {
|
if config.Webserver.Enable {
|
||||||
case "neighbours":
|
if config.Webserver.WebsocketNode {
|
||||||
result := &data.NeighbourStruct{}
|
wsserverForNodes = websocketserver.NewServer("/nodes")
|
||||||
if json.Unmarshal(res.Raw, result) == nil {
|
go wsserverForNodes.Listen()
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
})
|
http.Handle("/", http.FileServer(http.Dir(config.Webserver.Webroot)))
|
||||||
go respondDaemon.ListenAndSend(collectInterval)
|
}
|
||||||
|
|
||||||
|
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
|
//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
|
// Wait for End
|
||||||
sigs := make(chan os.Signal, 1)
|
sigs := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
45
models/config.go
Normal file
45
models/config.go
Normal file
@ -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
|
||||||
|
}
|
1
webroot
1
webroot
@ -1 +0,0 @@
|
|||||||
Subproject commit fb78d78bd3089cfdeeb191f5401b538de926197c
|
|
Loading…
Reference in New Issue
Block a user