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 ( | ||||
| 	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) | ||||
|  | ||||
							
								
								
									
										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