-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
95 lines (84 loc) · 2.51 KB
/
Copy pathmain.go
File metadata and controls
95 lines (84 loc) · 2.51 KB
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
package main
import (
"context"
"github.com/encore-fm/backend/config"
"github.com/encore-fm/backend/db"
"github.com/encore-fm/backend/events"
"github.com/encore-fm/backend/garbagecoll"
"github.com/encore-fm/backend/playerctrl"
"github.com/encore-fm/backend/server"
"github.com/encore-fm/backend/spotifycl"
_ "github.com/heroku/x/hmetrics/onload"
log "github.com/sirupsen/logrus"
"github.com/zmb3/spotify"
)
func spotifyAuthSetup() spotify.Authenticator {
spotifyAuth := spotify.NewAuthenticator(
config.Conf.Spotify.RedirectUrl,
spotify.ScopeStreaming,
spotify.ScopeUserReadEmail,
spotify.ScopeUserModifyPlaybackState,
spotify.ScopeUserReadPrivate,
spotify.ScopeUserReadPlaybackState,
spotify.ScopeUserTopRead,
)
spotifyAuth.SetAuthInfo(
config.Conf.Spotify.ClientID,
config.Conf.Spotify.ClientSecret,
)
return spotifyAuth
}
func main() {
config.Setup()
// init event bus
eventBus := events.NewEventBus()
eventBus.Start()
// connect to database
dbConn, err := db.New()
if err != nil {
panic(err)
}
userDB := db.NewUserCollection(dbConn.Client)
sessDB := db.NewSessionCollection(dbConn.Client)
songDB := db.NewSongCollection(dbConn.Client)
playerDB := db.NewPlayerCollection(dbConn.Client)
log.Infof(
"[startup] successfully connected to database at %v:%v",
config.Conf.Database.DBHost,
config.Conf.Database.DBPort,
)
// reset nr of active sse connections to 0
// required in case there were active sse connections before server startup/reset
err = userDB.ResetSSEConnections(context.Background())
if err != nil {
log.Fatalf("[startup] resetting sse connections: %v", err)
}
// create spotify client
spotifyClient, err := spotifycl.New(config.Conf.Spotify.ClientID, config.Conf.Spotify.ClientSecret)
if err != nil {
log.Fatalf("[startup] creating spotify client: %v", err)
}
spotifyClient.Start()
log.Info("[startup] successfully connected to spotify api")
// create spotify authenticator
spotifyAuth := spotifyAuthSetup()
// create controller
playerCtrl := playerctrl.NewController(
eventBus,
sessDB,
songDB,
userDB,
playerDB,
spotifyAuth,
)
if err := playerCtrl.Start(); err != nil {
log.Fatalf("[startup] starting player controller: %v", err)
}
log.Info("[startup] successfully started player controller")
gc := garbagecoll.New(userDB, sessDB, eventBus)
gc.Start()
log.Info("[startup] successfully started session garbage collector")
// start server
svr := server.New(eventBus, userDB, sessDB, songDB, playerDB, spotifyAuth, spotifyClient)
svr.Start()
}