/
main.go
72 lines (62 loc) · 1.48 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
72
package main
import (
"log"
"net/http"
"net/url"
"os"
"runtime"
"strings"
"time"
"github.com/runway7/satellite/Godeps/_workspace/src/github.com/garyburd/redigo/redis"
"github.com/runway7/satellite/Godeps/_workspace/src/github.com/julienschmidt/httprouter"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
redisUrlKey := os.Getenv("SATELLITE_REDIS_URL_KEY")
redisURL := os.Getenv(redisUrlKey)
if redisURL == "" {
redisURL = "localhost:6379"
}
u, err := url.Parse(redisURL)
if err != nil {
panic(err)
}
redisHost := u.Host
redisPassword, _ := u.User.Password()
pool := newPool(redisHost, redisPassword)
token := os.Getenv("TOKEN")
broadcaster := NewSatelliteHandler(pool, token)
stats := NewStatsHandler(pool)
router := httprouter.New()
router.HandlerFunc("GET", "/", stats)
router.HandlerFunc("GET", "/:channel", broadcaster)
router.HandlerFunc("POST", "/:channel", broadcaster)
port := strings.TrimSpace(os.Getenv("PORT"))
if port == "" {
port = "3001"
}
http.ListenAndServe(":"+port, router)
}
func newPool(host, password string) *redis.Pool {
return &redis.Pool{
MaxIdle: 2,
MaxActive: 5,
IdleTimeout: 5 * time.Second,
Wait: true,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", host)
if err != nil {
log.Println(err)
return nil, err
}
if password != "" {
if _, err := c.Do("AUTH", password); err != nil {
log.Println(err)
c.Close()
return nil, err
}
}
return c, err
},
}
}