-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (104 loc) · 2.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
// Message gets exchanged between users through redis pub/sub messaging
// Users may have websocket connections to different nodes and stored in
// different instances of this application
type Message struct {
DeliveryID string `json:"id"`
Content string `json:"content"`
}
const (
apiHostname = "api.dota.kyem.media"
liveMatchEndpoint = "/live/stats"
channelLiveMatchPrefix = "dota_live_match."
publishRate = 2
channelRate = 1
)
var hostname = apiHostname
var ctx = context.Background()
func main() {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
v := os.Getenv("DOTA_TV_API_HOSTNAME")
if len(v) > 3 {
hostname = v
}
fmt.Printf("Connecting to %s\n", hostname)
defer func(client *redis.Client) {
err := client.Close()
if err != nil {
fmt.Printf("Error in closing redis client %s", err.Error())
}
}(client)
var channels []string
var err error
ticker := time.NewTicker(channelRate * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
channels, err = client.PubSubChannels(ctx, channelLiveMatchPrefix+"*").Result()
if err != nil {
fmt.Printf("Error in receiving PubSubChannels: %s", err.Error())
}
case <-quit:
ticker.Stop()
return
}
}
}()
publishTicker := time.NewTicker(publishRate * time.Second)
publicQuit := make(chan struct{})
go func() {
for {
select {
case <-publishTicker.C:
for _, channelName := range channels {
go publishMatchData(client, channelName)
}
case <-publicQuit:
publishTicker.Stop()
return
}
}
}()
select {}
}
func publishMatchData(client *redis.Client, channelName string) {
s := strings.Split(channelName, ".")
url := fmt.Sprintf("http://%s%s?server_steam_id=%s", hostname, liveMatchEndpoint, s[1])
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
return
}
if resp.Body == nil {
fmt.Println("Please send a request body")
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println("Failed to close body", err.Error())
}
}(resp.Body)
contents, bodyErr := ioutil.ReadAll(resp.Body)
if bodyErr != nil {
fmt.Println("Error on request body")
return
}
cmd := client.Publish(ctx, channelName, string(contents))
fmt.Printf("Channel %s, published to %d users \n", channelName, cmd.Val())
}