@@ -11,41 +11,18 @@ import (
11
11
"os/signal"
12
12
"strings"
13
13
"syscall"
14
- "time"
15
14
16
15
"github.com/bafto/FindFavouriteSong/db"
17
16
"github.com/gorilla/securecookie"
18
17
"github.com/gorilla/sessions"
19
- "github.com/spf13/viper"
20
18
"github.com/zmb3/spotify/v2"
21
19
spotifyauth "github.com/zmb3/spotify/v2/auth"
22
20
)
23
21
24
- func read_config () {
25
- viper .SetDefault ("spotify_client_id" , "" )
26
- viper .SetDefault ("spotify_client_secret" , "" )
27
- viper .SetDefault ("db_path" , "ffs.db" )
28
- viper .SetDefault ("port" , "8080" )
29
- viper .SetDefault ("log_level" , "INFO" )
30
- viper .SetDefault ("redirect_url" , "http://localhost:8080/spotifyauthentication" )
31
- viper .SetDefault ("shutdown_timeout" , time .Second * 10 )
32
-
33
- viper .SetEnvPrefix ("ffs" )
34
- viper .AutomaticEnv ()
35
-
36
- viper .AddConfigPath ("." )
37
- viper .SetConfigName ("ffs_config" )
38
- viper .SetConfigType ("yaml" )
39
-
40
- if err := viper .ReadInConfig (); err != nil {
41
- panic (err )
42
- }
43
- }
44
-
45
22
func configure_slog () {
46
23
var level slog.Level
47
24
if err := level .UnmarshalText (
48
- []byte (viper . GetString ( "log_level" ) ),
25
+ []byte (config . Log_level ),
49
26
); err != nil {
50
27
panic (err )
51
28
}
@@ -72,6 +49,8 @@ type ActiveUser struct {
72
49
}
73
50
74
51
var (
52
+ config Config
53
+
75
54
selectSongsHtml = template .Must (template .ParseFiles ("select_songs.html" ))
76
55
selectPlaylistHtml = template .Must (template .ParseFiles ("select_playlist.html" ))
77
56
winnerHtml = template .Must (template .ParseFiles ("winner.html" ))
@@ -92,17 +71,20 @@ var (
92
71
)
93
72
94
73
func main () {
95
- read_config ()
74
+ var err error
75
+ config , err = read_config ()
76
+ if err != nil {
77
+ panic (err )
78
+ }
96
79
configure_slog ()
97
80
98
81
spotifyAuth = spotifyauth .New (
99
- spotifyauth .WithRedirectURL (viper . GetString ( "redirect_url" ) ),
82
+ spotifyauth .WithRedirectURL (config . Redirect_url ),
100
83
spotifyauth .WithScopes (spotifyauth .ScopePlaylistReadPrivate , spotifyauth .ScopeUserReadPrivate ),
101
- spotifyauth .WithClientSecret (viper . GetString ( "SPOTIFY_CLIENT_SECRET" ) ),
102
- spotifyauth .WithClientID (viper . GetString ( "SPOTIFY_CLIENT_ID" ) ),
84
+ spotifyauth .WithClientSecret (config . Spotify_client_secret ),
85
+ spotifyauth .WithClientID (config . Spotify_client_id ),
103
86
)
104
87
105
- var err error
106
88
db_conn , err = create_db (ctx , "ffs.db" )
107
89
if err != nil {
108
90
slog .Error ("Error opening DB connection" , "err" , err )
@@ -127,7 +109,7 @@ func main() {
127
109
mux .HandleFunc ("/winner" , withMiddleware (winnerHandler ))
128
110
mux .HandleFunc ("/stats" , withMiddleware (statsPageHandler ))
129
111
130
- server := & http.Server {Addr : ":" + viper . GetString ( "port" ) , Handler : mux }
112
+ server := & http.Server {Addr : ":" + config . Port , Handler : mux }
131
113
132
114
go func () {
133
115
if err := server .ListenAndServe (); ! errors .Is (err , http .ErrServerClosed ) {
@@ -143,7 +125,7 @@ func main() {
143
125
sig := <- sigChan
144
126
slog .Warn ("received signal, shutting down server" , "signal" , sig .String ())
145
127
146
- shutdownCtx , cancelShutdown := context .WithTimeout (ctx , viper . GetDuration ( "shutdown_timeout" ) )
128
+ shutdownCtx , cancelShutdown := context .WithTimeout (ctx , config . Shutdown_timeout )
147
129
defer cancelShutdown ()
148
130
149
131
if err := server .Shutdown (shutdownCtx ); err != nil {
0 commit comments