|
|
@@ -7,51 +7,53 @@ import (
|
|
|
|
|
|
"github.com/felamaslen/gmus-backend/pkg/config"
|
|
|
"github.com/felamaslen/gmus-backend/pkg/logger"
|
|
|
- "github.com/felamaslen/gmus-backend/pkg/read"
|
|
|
"github.com/go-redis/redis"
|
|
|
"github.com/gorilla/mux"
|
|
|
"github.com/rs/cors"
|
|
|
)
|
|
|
|
|
|
-func StartServer() {
|
|
|
- conf := config.GetConfig()
|
|
|
- l := logger.CreateLogger(conf.LogLevel)
|
|
|
+func (s *Server) Init() {
|
|
|
+ s.l = logger.CreateLogger(config.GetConfig().LogLevel)
|
|
|
+ s.router = mux.NewRouter()
|
|
|
|
|
|
- rdb := redis.NewClient(&redis.Options{Addr: conf.RedisUrl})
|
|
|
- defer rdb.Close()
|
|
|
+ healthRoutes(s.l, s.router)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *Server) Listen() {
|
|
|
+ conf := config.GetConfig()
|
|
|
|
|
|
- if conf.LibraryWatch {
|
|
|
- l.Info("Watching library for changes")
|
|
|
- go read.WatchLibraryRecursive(l, conf.LibraryDirectory)
|
|
|
- } else {
|
|
|
- l.Verbose("Not watching library for changes")
|
|
|
- }
|
|
|
+ handler := cors.New(cors.Options{
|
|
|
+ AllowedOrigins: conf.AllowedOrigins,
|
|
|
+ AllowCredentials: true,
|
|
|
+ }).Handler(s.router)
|
|
|
|
|
|
- router := mux.NewRouter()
|
|
|
+ s.l.Info("Starting server on %s:%d\n", conf.Host, conf.Port)
|
|
|
+ log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Host, conf.Port), handler))
|
|
|
+}
|
|
|
|
|
|
- healthRoutes(l, router)
|
|
|
+func StartServer() {
|
|
|
+ conf := config.GetConfig()
|
|
|
+ l := logger.CreateLogger(conf.LogLevel)
|
|
|
|
|
|
- initPubsub(l, rdb, router)
|
|
|
+ server := Server{}
|
|
|
+ server.Init()
|
|
|
|
|
|
- router.Path("/stream").Methods("GET").HandlerFunc(routeHandler(l, rdb, streamSong))
|
|
|
+ rdb := redis.NewClient(&redis.Options{Addr: conf.RedisUrl})
|
|
|
+ defer rdb.Close()
|
|
|
|
|
|
- router.Path("/artists").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchArtists))
|
|
|
- router.Path("/albums").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchAlbums))
|
|
|
- router.Path("/songs").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchSongs))
|
|
|
+ initPubsub(l, rdb, server.router)
|
|
|
|
|
|
- router.Path("/song-info").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchSongInfo))
|
|
|
- router.Path("/multi-song-info").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchMultiSongInfo))
|
|
|
+ server.router.Path("/stream").Methods("GET").HandlerFunc(routeHandler(l, rdb, streamSong))
|
|
|
|
|
|
- router.Path("/next-song").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchNextSong))
|
|
|
- router.Path("/prev-song").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchPrevSong))
|
|
|
+ server.router.Path("/artists").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchArtists))
|
|
|
+ server.router.Path("/albums").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchAlbums))
|
|
|
+ server.router.Path("/songs").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchSongs))
|
|
|
|
|
|
- port := conf.Port
|
|
|
+ server.router.Path("/song-info").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchSongInfo))
|
|
|
+ server.router.Path("/multi-song-info").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchMultiSongInfo))
|
|
|
|
|
|
- handler := cors.New(cors.Options{
|
|
|
- AllowedOrigins: conf.AllowedOrigins,
|
|
|
- AllowCredentials: true,
|
|
|
- }).Handler(router)
|
|
|
+ server.router.Path("/next-song").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchNextSong))
|
|
|
+ server.router.Path("/prev-song").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchPrevSong))
|
|
|
|
|
|
- l.Info("Starting server on %s:%d\n", conf.Host, port)
|
|
|
- log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Host, port), handler))
|
|
|
+ server.Listen()
|
|
|
}
|