switch to config-file

This commit is contained in:
Martin Geno 2016-03-07 09:52:52 +01:00
parent 646f8c5a9a
commit 5df9ee8f0e
5 changed files with 116 additions and 57 deletions

4
.gitmodules vendored
View File

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

50
main.go
View File

@ -18,37 +18,38 @@ import (
) )
var ( var (
wsserverForNodes = websocketserver.NewServer("/nodes") configFile string
config *models.Config
wsserverForNodes *websocketserver.Server
respondDaemon *respond.Daemon respondDaemon *respond.Daemon
nodes = models.NewNodes() nodes = models.NewNodes()
aliases = models.NewNodes() aliases = models.NewNodes()
listenAddr string
listenPort string
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)
if config.Nodes.Enable {
go nodes.Saver(config.Nodes.NodesPath, saveInterval)
}
if config.Nodes.AliasesEnable {
go aliases.Saver(config.Nodes.AliasesPath, saveInterval)
}
if config.Webserver.Enable {
if config.Webserver.WebsocketNode {
wsserverForNodes = websocketserver.NewServer("/nodes")
go wsserverForNodes.Listen() go wsserverForNodes.Listen()
go nodes.Saver(outputNodesFile, saveInterval) }
go aliases.Saver(outputAliasesFile, saveInterval) http.Handle("/", http.FileServer(http.Dir(config.Webserver.Webroot)))
}
if config.Responedd.Enable {
respondDaemon = respond.NewDaemon(func(coll *respond.Collector, res *respond.Response) { respondDaemon = respond.NewDaemon(func(coll *respond.Collector, res *respond.Response) {
switch coll.CollectType { switch coll.CollectType {
@ -75,11 +76,12 @@ func main() {
} }
}) })
go respondDaemon.ListenAndSend(collectInterval) 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
View 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 +0,0 @@
Subproject commit fb78d78bd3089cfdeeb191f5401b538de926197c