-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (66 loc) · 2.41 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/m1stborn/mistChatbot/internal/pkg/line"
"github.com/m1stborn/mistChatbot/internal/pkg/model"
"github.com/m1stborn/mistChatbot/internal/pkg/twitchmod"
"github.com/m1stborn/mistChatbot/internal/pkg/youtubemod"
_ "github.com/joho/godotenv/autoload"
)
var err error
var (
port = os.Getenv("PORT")
dbUri = os.Getenv("DB_URI")
)
var (
testStreamer = []string{"muse_tw", "lck", "dogdog", "lolpacifictw", "m989876525", "qq7925168", "never_loses"}
testLine = os.Getenv("TEST_LINE_USER")
testAccessToken = os.Getenv("TEST_LINE_NOTIFY_ACCESSTOKEN")
)
func main() {
//step 1: init DB
model.DB.Init(dbUri)
//step 1.1: Create http router for line webhook
http.HandleFunc("/line", line.Handler)
http.HandleFunc("/line/notify/auth", line.HandelNotifyAuth)
http.HandleFunc("/line/notify/callback", line.HandleNotifyCallback)
//step 1.2: Create http router for twitch webhook
http.HandleFunc("/twitch/channelFollow", twitchmod.EventSubFollow)
http.HandleFunc("/twitch/streamOnline", twitchmod.EventSubStreamOnline)
//step 1.2: Create http router for YouTube PubSubHubBub webhook
http.HandleFunc("/youtube/pubsub/", youtubemod.PubSub.HandlePubSubCallback)
//step 2.1: init Twitch Client
//twitch := nweTwitch()
//step 2.2: delete old subscription during develop and testing
//subIds := twitchmod.GetSubscriptions()
//twitchmod.DeleteSubscriptions(subIds)
//step 2.3: create Event subscriptions
err = twitchmod.CreateChannelFollowSubscription("twitch", "/twitch/channelFollow")
err = twitchmod.CreateStreamOnlineSubscription("twitch", "/twitch/streamOnline")
for _, streamer := range testStreamer {
err = twitchmod.CreateStreamOnlineSubscription(streamer, "/twitch/streamOnline")
model.DB.CreateSubscription(&model.Subscription{
Line: testLine,
LineAccessToken: testAccessToken,
TwitchLoginName: streamer,
})
}
if err != nil {
fmt.Println(err)
}
//step 3.1: restore PubSubClient from DB
var oldPubSubs []model.PubSubSubscription
oldPubSubs = model.DB.QueryAllPubSub()
for _, old := range oldPubSubs {
youtubemod.PubSub.RestoreSubscribe(old.Topic, old.CallbackId, youtubemod.FeedHandler)
}
//step 3.2: init YouTube Tracker
youtubemod.Tracker.Init()
go youtubemod.Tracker.StartTrack()
//step 4: start up server
fmt.Println("Starting the webserver listen on port", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}