server.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package server
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "github.com/felamaslen/gmus-backend/pkg/config"
  7. "github.com/felamaslen/gmus-backend/pkg/logger"
  8. "github.com/go-redis/redis/v7"
  9. "github.com/gorilla/mux"
  10. "github.com/rs/cors"
  11. )
  12. func StartServer() {
  13. conf := config.GetConfig()
  14. l := logger.CreateLogger(conf.LogLevel)
  15. rdb := redis.NewClient(&redis.Options{Addr: conf.RedisUrl})
  16. defer rdb.Close()
  17. router := mux.NewRouter()
  18. initPubsub(l, rdb, router)
  19. router.Path("/stream").Methods("GET").HandlerFunc(routeHandler(l, rdb, streamSong))
  20. router.Path("/artists").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchArtists))
  21. router.Path("/albums").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchAlbums))
  22. router.Path("/songs").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchSongs))
  23. router.Path("/song-info").Methods("GET").HandlerFunc(routeHandler(l, rdb, routeFetchSongInfo))
  24. port := conf.Port
  25. handler := cors.AllowAll().Handler(router)
  26. l.Info("Starting server on port %d\n", port)
  27. log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", port), handler))
  28. }