-
Notifications
You must be signed in to change notification settings - Fork 41
/
tumlive.go
executable file
·261 lines (231 loc) · 6.74 KB
/
tumlive.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
import (
"fmt"
"log/slog"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"time"
"github.com/TUM-Dev/gocast/api"
"github.com/TUM-Dev/gocast/dao"
"github.com/TUM-Dev/gocast/model"
"github.com/TUM-Dev/gocast/tools"
"github.com/TUM-Dev/gocast/tools/tum"
"github.com/TUM-Dev/gocast/web"
"github.com/dgraph-io/ristretto"
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
slogGorm "github.com/orandin/slog-gorm"
"github.com/pkg/profile"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var VersionTag = "development"
type initializer func()
var logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
})).With("service", "main")
var initializers = []initializer{
tools.LoadConfig,
api.ServeWorkerGRPC,
tools.InitBranding,
}
func initAll(initializers []initializer) {
for _, init := range initializers {
init()
}
}
// GinServer launches the gin server
func GinServer() (err error) {
router := gin.New()
router.Use(gin.Recovery())
gin.SetMode(gin.ReleaseMode)
// capture performance with sentry
router.Use(sentrygin.New(sentrygin.Options{Repanic: true}))
if VersionTag != "development" {
tools.CookieSecure = true
}
router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
if param.StatusCode >= 400 {
return fmt.Sprintf("{\"service\": \"GIN\", \"time\": %s, \"status\": %d, \"client\": \"%s\", \"path\": \"%s\", \"agent\": %s}\n",
param.TimeStamp.Format(time.DateTime),
param.StatusCode,
param.ClientIP,
param.Path,
param.Request.UserAgent(),
)
}
return ""
}))
router.Use(tools.InitContext(dao.NewDaoWrapper()))
liveUpdates := router.Group("/api/pub-sub")
api.ConfigRealtimeRouter(liveUpdates)
// event streams don't work with gzip, configure group without
chat := router.Group("/api/chat")
api.ConfigChatRouter(chat)
router.Use(gzip.Gzip(gzip.DefaultCompression))
api.ConfigGinRouter(router)
web.ConfigGinRouter(router)
err = router.Run(":8081")
// err = router.RunTLS(":443", tools.Cfg.Saml.Cert, tools.Cfg.Saml.Privkey)
if err != nil {
sentry.CaptureException(err)
logger.Error("Error starting tumlive", "err", err)
}
return
}
var osSignal chan os.Signal
func main() {
initAll(initializers)
defer profile.Start(profile.MemProfile).Stop()
go func() {
_ = http.ListenAndServe(":8082", nil) // debug endpoint
}()
// log with time, fmt "23.09.2021 10:00:00"
// log.SetFormatter(&log.TextFormatter{TimestampFormat: "02.01.2006 15:04:05", FullTimestamp: true})
web.VersionTag = VersionTag
osSignal = make(chan os.Signal, 1)
env := "production"
if VersionTag == "development" {
env = "development"
}
if os.Getenv("SentryDSN") != "" {
err := sentry.Init(sentry.ClientOptions{
Dsn: os.Getenv("SentryDSN"),
Release: VersionTag,
TracesSampleRate: 0.15,
Debug: true,
AttachStacktrace: true,
Environment: env,
})
if err != nil {
logger.Error("sentry.Init", "err", err)
}
// Flush buffered events before the program terminates.
defer sentry.Flush(2 * time.Second)
defer sentry.Recover()
}
gormJSONLogger := slogGorm.New()
db, err := gorm.Open(mysql.Open(fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?parseTime=true&loc=Local",
tools.Cfg.Db.User,
tools.Cfg.Db.Password,
tools.Cfg.Db.Host,
tools.Cfg.Db.Port,
tools.Cfg.Db.Database),
), &gorm.Config{
PrepareStmt: true,
Logger: gormJSONLogger,
})
if err != nil {
sentry.CaptureException(err)
sentry.Flush(time.Second * 5)
logger.Error("Error opening database", "err", err)
}
dao.DB = db
err = dao.Migrator.RunBefore(db)
if err != nil {
logger.Error("Error running before db", "err", err)
return
}
err = db.AutoMigrate(
&model.User{},
&model.Course{},
&model.Chat{},
&model.RegisterLink{},
&model.Silence{},
&model.ShortLink{},
&model.Stat{},
&model.StreamUnit{},
&model.LectureHall{},
&model.IngestServer{},
&model.StreamName{},
&model.Stream{},
&model.Worker{},
&model.CameraPreset{},
&model.ServerNotification{},
&model.File{},
&model.StreamProgress{},
&model.Token{},
&model.Poll{},
&model.PollOption{},
&model.VideoSection{},
&model.VideoSeekChunk{},
&model.Notification{},
&model.UploadKey{},
&model.UserSetting{},
&model.Audit{},
&model.InfoPage{},
&model.Bookmark{},
&model.TranscodingProgress{},
&model.ChatReaction{},
&model.Subtitles{},
&model.TranscodingFailure{},
&model.Email{},
)
if err != nil {
sentry.CaptureException(err)
sentry.Flush(time.Second * 5)
logger.Error("can't migrate database", "err", err)
}
err = dao.Migrator.RunAfter(db)
if err != nil {
logger.Error("Error running after db", "err", err)
return
}
// tools.SwitchPreset()
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
Metrics: true,
})
if err != nil {
sentry.CaptureException(err)
sentry.Flush(time.Second * 5)
logger.Error("Error risretto.NewCache", "err", err)
}
dao.Cache = *cache
// init meili search index settings
go tools.NewMeiliExporter(dao.NewDaoWrapper()).SetIndexSettings()
mailer := tools.NewMailer(dao.NewDaoWrapper(), tools.Cfg.Mail.MaxMailsPerMinute)
go mailer.Run()
initCron()
go func() {
err = GinServer()
if err != nil {
sentry.CaptureException(err)
sentry.Flush(time.Second * 5)
logger.Error("can't launch gin server", "err", err)
}
}()
keepAlive()
}
func initCron() {
daoWrapper := dao.NewDaoWrapper()
tools.InitCronService()
// Fetch students every 12 hours
_ = tools.Cron.AddFunc("fetchCourses", tum.FetchCourses(daoWrapper), "0 */12 * * *")
// Collect livestream stats (viewers) every minute
_ = tools.Cron.AddFunc("collectStats", api.CollectStats(daoWrapper), "0-59 * * * *")
// Flush stale sentry exceptions and transactions every 5 minutes
_ = tools.Cron.AddFunc("sentryFlush", func() { sentry.Flush(time.Minute * 2) }, "0-59/5 * * * *")
// Look for due streams and notify workers about them
_ = tools.Cron.AddFunc("triggerDueStreams", api.NotifyWorkers(daoWrapper), "0-59 * * * *")
// update courses available
_ = tools.Cron.AddFunc("prefetchCourses", tum.PrefetchCourses(daoWrapper), "30 3 * * *")
// export data to meili search
_ = tools.Cron.AddFunc("exportToMeili", tools.NewMeiliExporter(daoWrapper).Export, "30 4 * * *")
// fetch live stream previews
_ = tools.Cron.AddFunc("fetchLivePreviews", api.FetchLivePreviews(daoWrapper), "*/1 * * * *")
tools.Cron.Run()
}
func keepAlive() {
signal.Notify(osSignal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)
s := <-osSignal
logger.Info("Exiting on signal" + s.String())
}