-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
101 lines (83 loc) · 2.54 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"flag"
"net/http"
"os"
"os/signal"
"syscall"
"time"
yanic "github.com/FreifunkBremen/yanic/database/socket/client"
runtimeYanic "github.com/FreifunkBremen/yanic/runtime"
"github.com/NYTimes/gziphandler"
httpLib "github.com/genofire/golang-lib/http"
"github.com/genofire/golang-lib/log"
"github.com/genofire/golang-lib/worker"
configPackage "github.com/FreifunkBremen/freifunkmanager/config"
"github.com/FreifunkBremen/freifunkmanager/runtime"
"github.com/FreifunkBremen/freifunkmanager/ssh"
"github.com/FreifunkBremen/freifunkmanager/websocket"
)
var (
configFile string
config *configPackage.Config
nodes *runtime.Nodes
commands *runtime.Commands
yanicDialer *yanic.Dialer
stats *runtimeYanic.GlobalStats
)
func main() {
flag.StringVar(&configFile, "config", "config.conf", "path of configuration file (default:config.conf)")
flag.Parse()
config = configPackage.ReadConfigFile(configFile)
log.Log.Info("starting...")
sshmanager := ssh.NewManager(config.SSHPrivateKey)
nodes = runtime.NewNodes(config.StatePath, config.SSHInterface, sshmanager)
commands = runtime.NewCommands(sshmanager)
nodesUpdateWorker := worker.NewWorker(time.Duration(3)*time.Minute, nodes.Updater)
nodesSaveWorker := worker.NewWorker(time.Duration(3)*time.Second, nodes.Saver)
go nodesUpdateWorker.Start()
go nodesSaveWorker.Start()
websocket.Start(nodes, commands)
if config.Yanic.Enable {
yanicDialer := yanic.Dial(config.Yanic.Type, config.Yanic.Address)
yanicDialer.NodeHandler = nodes.LearnNode
yanicDialer.GlobalsHandler = func(data *runtimeYanic.GlobalStats) {
stats = data
websocket.NotifyStats(data)
}
go yanicDialer.Start()
}
// Startwebserver
http.HandleFunc("/nodes", func(w http.ResponseWriter, r *http.Request) {
httpLib.Write(w, nodes)
log.HTTP(r).Info("done")
})
http.HandleFunc("/stats", func(w http.ResponseWriter, r *http.Request) {
httpLib.Write(w, stats)
log.HTTP(r).Info("done")
})
http.Handle("/", gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webroot))))
srv := &http.Server{
Addr: config.WebserverBind,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Log.Panic(err)
}
}()
log.Log.Info("started")
// Wait for system signal
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
// Stop services
websocket.Close()
srv.Close()
if config.Yanic.Enable {
yanicDialer.Close()
}
nodesSaveWorker.Close()
nodesUpdateWorker.Close()
sshmanager.Close()
log.Log.Info("stop recieve:", sig)
}