This repository has been archived by the owner on Apr 3, 2022. It is now read-only.
/
geobin.go
100 lines (82 loc) · 2.29 KB
/
geobin.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
// Package geobin.io runs a web server which creates a geobin url that can receive geo data via POSTs and
// visualizes it on a map.
package main
import (
"flag"
"fmt"
"log"
"net/http"
"runtime"
"github.com/go-redis/redis"
)
// some read-only global vars
var isDebug = flag.Bool("debug", false, "Boolean flag indicates a debug build. Affects log statements.")
var isVerbose = flag.Bool("verbose", false, "Boolean flag indicates you want to see a lot of log messages.")
func init() {
// add file info to log statements
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
}
// starts the redis pump and http server
func main() {
flag.Parse()
// TODO: verify if this is actually beneficial
runtime.GOMAXPROCS(runtime.NumCPU())
// load up config.json
conf := loadConfig()
// redis client
client := redis.NewTCPClient(&redis.Options{
Addr: conf.RedisHost,
Password: conf.RedisPass,
DB: conf.RedisDB,
})
if ping := client.Ping(); ping.Err() != nil {
log.Fatal(ping.Err())
}
// redis pubsub connection
ps := client.PubSub()
// prepare a socketmap
sm := NewSocketMap(ps)
// loop for receiving messages from Redis pubsub, and forwarding them on to relevant ws connection
go redisPump(ps, sm)
defer func() {
ps.Close()
client.Close()
}()
// prepare server
http.Handle("/", NewGeobinServer(conf, NewRedisWrapper(client), ps, sm))
// Start up HTTP server
log.Println("Starting server at", conf.Host, conf.Port)
err := http.ListenAndServe(fmt.Sprintf("%v:%d", conf.Host, conf.Port), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
// redisPump reads messages out of redis and pushes them through the
// appropriate websocket
func redisPump(ps *redis.PubSub, sm SocketMap) {
for {
v, err := ps.Receive()
if err != nil {
log.Println("Error from Redis PubSub:", err)
return
}
switch v := v.(type) {
case *redis.Message:
if err = sm.Send(v.Channel, []byte(v.Payload)); err != nil {
log.Println(err)
}
}
}
}
// debugLog logs messages sent to it if and only if isDebug or isVerbose are set to true
func debugLog(v ...interface{}) {
if *isDebug || *isVerbose {
log.Println(v...)
}
}
// verboseLog logs messages sent to it if and only if isVerbose is set to true
func verboseLog(v ...interface{}) {
if *isVerbose {
log.Println(v...)
}
}