This commit is contained in:
Martin Geno 2016-02-19 11:30:42 +01:00
parent 708e8c4563
commit b74a2480cd
3 changed files with 40 additions and 11 deletions

View File

@ -12,7 +12,7 @@ type Announced struct {
collectors []*Collector collectors []*Collector
} }
func NewAnnounced(ns NodeServer) *Announced { func NewAnnounced(ns *NodeServer) *Announced {
collects := []*Collector{ collects := []*Collector{
NewCollector("statistics"), NewCollector("statistics"),
NewCollector("nodeinfo"), NewCollector("nodeinfo"),
@ -21,7 +21,7 @@ func NewAnnounced(ns NodeServer) *Announced {
return &Announced{ return &Announced{
ns, ns,
NewNodes(), NewNodes(),
output, "webroot/nodes.json",
time.Second * time.Duration(15), time.Second * time.Duration(15),
time.Second * time.Duration(15), time.Second * time.Duration(15),
collects, collects,

33
main.go
View File

@ -1,19 +1,42 @@
package main package main
import ( import (
"flag"
"log" "log"
"time"
"net/http" "net/http"
) )
var (
nodeserver = NewNodeServer("/nodes")
nodes = NewNodes()
outputFile string
collectInterval time.Duration
saveInterval time.Duration
)
func main(){ func main(){
node := NewNodeServer("/nodes") var collectSeconds, saveSeconds int
go node.Listen()
annouced := NewAnnouced(node) flag.StringVar(&outputFile, "output", "nodes.json", "path output file")
go annouced.Run() flag.IntVar(&collectSeconds, "collectInterval", 15, "interval for data collections")
flag.IntVar(&saveSeconds, "saveInterval", 5, "interval for data saving")
flag.Parse()
collectInterval = time.Second * time.Duration(collectSeconds)
saveInterval = time.Second * time.Duration(saveSeconds)
collectors := []*Collector{
NewCollector("statistics"),
NewCollector("nodeinfo"),
NewCollector("neighbours"),
}
go nodeserver.Listen()
// static files // static files
http.Handle("/", http.FileServer(http.Dir("webroot"))) http.Handle("/", http.FileServer(http.Dir("webroot")))
log.Fatal(http.ListenAndServe(":8080", nil)) log.Fatal(http.ListenAndServe(":8080", nil))
for _, c := range collectors {
c.Close()
}
} }

View File

@ -70,12 +70,18 @@ func (nodes *Nodes) save() {
nodes.Unlock() nodes.Unlock()
if err !=nil{ if err !=nil{
log.Panic(e) log.Panic(err)
} }
log.Println("saving", len(nodes.List), "nodes") log.Println("saving", len(nodes.List), "nodes")
tmpFile := outputFile + ".tmp" tmpFile := outputFile + ".tmp"
check(ioutil.WriteFile(tmpFile, data, 0644)) err = ioutil.WriteFile(tmpFile, data, 0644)
check(os.Rename(tmpFile, outputFile)) if err !=nil{
log.Panic(err)
}
err = os.Rename(tmpFile, outputFile)
if err !=nil{
log.Panic(err)
}
} }