forked from geobin-io/geobin.io
/
geobin.go
92 lines (78 loc) · 2.08 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
// 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"
"math/rand"
"net/http"
"runtime"
"time"
redis "github.com/vmihailenco/redis/v2"
)
var config = &Config{}
var client = &redis.Client{}
var pubsub = &redis.PubSub{}
var socketMap SocketMap
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() {
flag.Parse()
// set numprocs
runtime.GOMAXPROCS(runtime.NumCPU())
// add file info to log statements
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
// set up unique seed for random num generation
rand.Seed(time.Now().UTC().UnixNano())
loadConfig()
setupRedis()
}
// starts the redis pump and http server
func main() {
// prepare router
r := createRouter()
http.Handle("/", r)
// loop for receiving messages from Redis pubsub, and forwarding them on to relevant ws connection
go redisPump()
defer func() {
pubsub.Close()
client.Close()
}()
// Start up HTTP server
log.Println("Starting server at", config.Host, config.Port)
err := http.ListenAndServe(fmt.Sprintf("%v:%d", config.Host, config.Port), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
// setupRedis creates a redis client and pubsub
func setupRedis() {
client = redis.NewTCPClient(&redis.Options{
Addr: config.RedisHost,
Password: config.RedisPass,
DB: config.RedisDB,
})
if ping := client.Ping(); ping.Err() != nil {
log.Fatal(ping.Err())
}
pubsub = client.PubSub()
socketMap = NewSocketMap(pubsub)
}
// redisPump reads messages out of redis and pushes them through the
// appropriate websocket
func redisPump() {
for {
v, err := pubsub.Receive()
if err != nil {
log.Println("Error from Redis PubSub:", err)
return
}
switch v := v.(type) {
case *redis.Message:
if err = socketMap.Send(v.Channel, []byte(v.Payload)); err != nil {
log.Println(err)
}
}
}
}