-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodnod.go
95 lines (73 loc) · 1.77 KB
/
nodnod.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
package main
import (
"flag"
"fmt"
log "github.com/Sirupsen/logrus"
"net/http"
"os"
)
const (
VERSION = "0.1"
CONFIG_PATH = "./conf/conf.json"
)
var (
flHelp = flag.Bool("help", false, "Print help!")
flAddress = flag.String("listen", "127.0.0.1:7070",
"Websocket service listen address")
flConfigPath = flag.String("config", CONFIG_PATH, "Path to configuration path.")
flName = flag.String("name", "",
"Name of this NodNod server. Default is Host name.")
flVersion = flag.Bool("version", false, "Show version!")
flDebug = flag.Bool("debug", false, "Set logging level to DEBUG!")
// flStatic = flag.Bool([]string{"s", "-static"}, false, "Serve static html demo")
globalConfig = new(Configuration)
globalNodes []*Node
)
func main() {
// 1. Handle cmd line args
flag.Usage = showHelp
flag.Parse()
if *flHelp {
flag.Usage()
return
}
log.SetLevel(log.InfoLevel)
if *flDebug {
log.SetLevel(log.DebugLevel)
}
if *flVersion {
showVersion()
return
}
if *flName == "" {
*flName, _ = os.Hostname()
log.Warn("Using hostname as NodNod name:", *flName)
}
// 2. Load and validate config
err := loadConfig()
if err != nil {
panic(err)
}
if err = loadNodes(); err != nil {
panic(err)
}
// 3. Start discovery
go discover()
// 4. Launch server
handler := new(WebsocketHandler)
http.Handle("/", handler)
log.Infof("Starting NodNod websocket server: [%s : %s]", *flName, *flAddress)
err = http.ListenAndServe(*flAddress, nil)
if err != nil {
log.Fatal("Failed to start server!")
}
}
func showVersion() {
fmt.Printf("NodNod version\t%s\n", VERSION)
}
func showHelp() {
fmt.Fprint(os.Stdout, "Usage: nodnod [OPTIONS]\n\n")
fmt.Fprint(os.Stdout, "OPTIONS:\n\n")
flag.CommandLine.SetOutput(os.Stdout)
flag.PrintDefaults()
}