-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
147 lines (127 loc) · 3.08 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"context"
"fmt"
"log"
"sync"
"github.com/Sadzeih/valcompbot/comments"
"github.com/Sadzeih/valcompbot/ent/pickemsevent"
"github.com/Sadzeih/valcompbot/matches"
"github.com/Sadzeih/valcompbot/pickems"
"github.com/Sadzeih/valcompbot/config"
"github.com/Sadzeih/valcompbot/ent"
"github.com/Sadzeih/valcompbot/ent/migrate"
"github.com/Sadzeih/valcompbot/highlighter"
"github.com/Sadzeih/valcompbot/internal/api"
_ "github.com/lib/pq"
"github.com/vartanbeno/go-reddit/v2/reddit"
)
func main() {
if err := config.Parse(); err != nil {
log.Print(err)
return
}
credentials := reddit.Credentials{
ID: config.Get().RedditClientID,
Secret: config.Get().RedditClientSecret,
Username: config.Get().RedditUsername,
Password: config.Get().RedditPassword,
}
redditClient, err := reddit.NewClient(credentials)
if err != nil {
log.Print(err)
return
}
entClient, err := ent.Open("postgres", config.Get().PostgresString)
if err != nil {
log.Print(err)
return
}
defer entClient.Close()
ctx := context.Background()
if err := entClient.Debug().Schema.Create(
ctx,
migrate.WithDropIndex(true),
migrate.WithDropColumn(true),
); err != nil {
log.Printf("failed creating schema resources: %v", err)
return
}
// A wait group for synchronizing routines
wg := sync.WaitGroup{}
wg.Add(1)
// Sidebar ticker routine
go func() {
defer wg.Done()
startSidebar(redditClient)
}()
// Comments topic routine
commentsTopic := comments.New(redditClient)
go func() {
defer wg.Done()
defer commentsTopic.Close()
commentsTopic.Run()
}()
// PMT scheduler
scheduler := matches.NewScheduler(entClient, redditClient)
go func() {
defer wg.Done()
scheduler.Start(ctx)
}()
// API routine
go func() {
defer wg.Done()
err := api.Start(redditClient, entClient)
if err != nil {
log.Printf("error while running API: %v", err)
return
}
}()
// Pickems routine
if config.Get().EnablePickems {
go func() {
defer wg.Done()
pickemsEvent, err := entClient.PickemsEvent.Query().
Order(ent.Desc(pickemsevent.FieldTimestamp)).
Limit(1).
Only(ctx)
if ent.MaskNotFound(err) != nil {
log.Print(fmt.Errorf("could not get latest pickems event"))
return
}
if pickemsEvent != nil {
pickems.Event = pickemsEvent.EventID
}
pickemsService := pickems.New(redditClient, commentsTopic.Subscribe())
if err := pickemsService.Run(); err != nil {
return
}
}()
}
// Comment highlighter routine
if config.Get().EnableStickies {
go func() {
defer wg.Done()
h, err := highlighter.New(context.Background(), redditClient, entClient, commentsTopic.Subscribe())
if err != nil {
log.Printf("failed creating highlighter: %v", err)
return
}
if err := h.Run(); err != nil {
log.Printf("failed to run highlighter: %v", err)
return
}
}()
}
// Sentinels routine
if config.Get().EnableSentinels {
go func() {
defer wg.Done()
if err := DaysSinceLastSentinelsPost(redditClient); err != nil {
log.Printf("Error in DaysSinceLastSentinelsPost: %v", err)
return
}
}()
}
wg.Wait()
}