-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (65 loc) · 1.58 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
package main
import (
"fmt"
"log"
"time"
b64 "encoding/base64"
"encoding/json"
api "github.com/angelthump/cache-main/api"
client "github.com/angelthump/cache-main/client"
server "github.com/angelthump/cache-main/server"
utils "github.com/angelthump/cache-main/utils"
)
func main() {
cfgPath, err := utils.ParseFlags()
if err != nil {
log.Fatal(err)
}
err = utils.NewConfig(cfgPath)
if err != nil {
log.Fatal(err)
}
client.Initalize()
saveStreams()
server.Initalize()
}
func saveStreams() {
streams := api.Find()
if streams == nil {
time.AfterFunc(1*time.Second, func() {
saveStreams()
})
return
}
for _, stream := range streams {
go func(stream api.Stream) {
user := api.GetUser(stream.User.Id)
if user == nil {
return
}
stream.User = *user
base64String := b64.StdEncoding.EncodeToString([]byte(stream.Created_at + stream.User.Username))
err := client.Rdb.Set(client.Ctx, stream.User.Username, base64String, 10*time.Second).Err()
if err != nil {
log.Println(err)
}
//set it for stream_key as well for mediamtx warmer
err = client.Rdb.Set(client.Ctx, stream.User.StreamKey, base64String+"_"+stream.User.Username, 10*time.Second).Err()
if err != nil {
log.Println(err)
}
marshalledStream, err := json.Marshal(stream)
if err != nil {
fmt.Println(err)
return
}
err = client.Rdb.Set(client.Ctx, base64String+"_"+stream.User.Username, string(marshalledStream), 10*time.Second).Err()
if err != nil {
log.Println(err)
}
}(stream)
}
time.AfterFunc(1*time.Second, func() {
saveStreams()
})
}