forked from fw42/honeymap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
honeymap.go
104 lines (88 loc) · 2.13 KB
/
honeymap.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
102
103
104
package main
import (
"encoding/json"
"github.com/fw42/go-hpfeeds"
"github.com/fzzy/sockjs-go/sockjs"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"runtime"
"path"
)
const staticDir = "../client"
const bind = "0.0.0.0:3000"
type Config struct {
Host string
Port int
Ident string
Auth string
}
func dirname() string {
_, myself, _, _ := runtime.Caller(1)
return path.Dir(myself)
}
func readConfig() Config {
blob, err := ioutil.ReadFile(dirname() + "/" + "config.json")
checkFatalError(err)
var conf Config
err = json.Unmarshal(blob, &conf)
checkFatalError(err)
return conf
}
func checkFatalError(err error) {
if err != nil {
log.Printf("%s\n", err)
os.Exit(-1)
}
}
var sockjsClients *sockjs.SessionPool = sockjs.NewSessionPool()
func dataHandler(s sockjs.Session) {
sockjsClients.Add(s)
defer sockjsClients.Remove(s)
for {
m := s.Receive()
if m == nil {
break
}
}
}
func broadcast(input chan hpfeeds.Message) {
for msg := range input {
sockjsClients.Broadcast(msg.Payload)
}
}
func hpfeedsConnect(config Config, geolocEvents chan hpfeeds.Message) {
backoff := 0
hp := hpfeeds.NewHpfeeds(config.Host, config.Port, config.Ident, config.Auth)
hp.Log = true
log.Printf("Connecting to %s:%d...\n", config.Host, config.Port)
for {
err := hp.Connect()
if err == nil {
log.Printf("Connected to Hpfeeds server.")
hp.Subscribe("geoloc.events", geolocEvents)
<-hp.Disconnected
log.Printf("Lost connection to %s:%d :-(\n", config.Host, config.Port)
}
log.Printf("Reconnecting to %s:%d\n", config.Host, config.Port)
time.Sleep(time.Duration(backoff) * time.Second)
if backoff <= 10 {
backoff++
}
}
}
func main() {
config := readConfig()
http.Handle("/", http.FileServer(http.Dir(dirname() + "/" + staticDir + "/")))
sockjsMux := sockjs.NewServeMux(http.DefaultServeMux)
sockjsConf := sockjs.NewConfig()
sockjsMux.Handle("/data", dataHandler, sockjsConf)
geolocEvents := make(chan hpfeeds.Message)
go hpfeedsConnect(config, geolocEvents)
go broadcast(geolocEvents)
log.Printf("Binding Honeymap webserver to %s...", bind)
err := http.ListenAndServe(bind, sockjsMux)
checkFatalError(err)
}