-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
71 lines (59 loc) · 1.4 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
package main
import (
"fmt"
"net"
"net/http"
"time"
"github.com/ninjasphere/go-ninja/config"
"github.com/ninjasphere/go-ninja/logger"
"github.com/ninjasphere/go-ninja/support"
"github.com/ninjasphere/sphere-ui/websocket"
)
var log *logger.Logger
func main() {
log = logger.GetLogger("sphere-ui")
fs := http.FileServer(http.Dir("public"))
http.Handle("/", fs)
iface := config.String("", "sphere-ui.interface")
if iface != "" {
i, err := net.InterfaceByName(iface)
if err != nil {
time.Sleep(time.Second * 5)
panic(err)
}
addrs, err := i.Addrs()
if err != nil {
time.Sleep(time.Second * 5)
panic(err)
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
iface = ip.String()
}
}
address := fmt.Sprintf("%s:%d", iface, config.Int(80, "sphere-ui.port"))
log.Infof("Listening on %s", address)
ws := websocket.WebsocketServer{
Port: config.Int(9001, "sphere-ui.websocket.port"),
}
if err := ws.PostConstruct(); err != nil {
log.FatalErrorf(err, "failed while starting websocket server")
}
if err := http.ListenAndServe(address, nil); err != nil {
log.FatalErrorf(err, "failed while listening http server")
}
support.WaitUntilSignal()
}