+github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
+github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
+github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
+github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
+github.com/redis/go-redis/v9 v9.18.0 h1:pMkxYPkEbMPwRdenAzUNyFNrDgHx9U+DrBabWNfSRQs=
+github.com/redis/go-redis/v9 v9.18.0/go.mod h1:k3ufPphLU5YXwNTUcCRXGxUoF1fqxnhFQmscfkCoDA0=
+github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
+github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
+go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
+go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
"log"
"net/http"
"os"
+ "songmanager/pkg/dbhandling"
+ "strconv"
- "github.com/gorilla/mux"
+ "github.com/rs/cors"
)
func main() {
// Getting the parameters
paramPort := flag.Int("port", 8000, "Specify the port you want to host this server on. By default: 8000")
paramBinbLocation := flag.String("binb_location", "", "Specify the folder containing the app.js for the binb version you are running")
- paramSongsLocation := flag.String("song_location", "", "Specify the /public/songs folder where the m4a preview files should be saved.")
+ paramSongsLocation := flag.String("songs_location", "", "Specify the /public/songs folder where the m4a preview files should be saved.")
paramRedisLocation := flag.String("redis_location", "", "Specify the location of the redis instance to open, probably a localhost place.")
paramContainerName := flag.String("container_name", "", "Specify the name of the container image that would have to be started / restarted")
flag.Parse()
// Setting up logging
- logFile, err := os.OpenFile("./log/sm.log", os.O_APPEND | os.O_RDWR, 0664)
+ logFile, err := os.OpenFile("./log/sm.log", os.O_APPEND | os.O_RDWR, 0666)
if err != nil {
fmt.Println("While setting up the server, it could not open the main log file at log/sm.log.", err)
panic("Failed to open log file.");
}
-
defer logFile.Close()
mainLogger := log.New(logFile, "", log.LstdFlags | log.Lshortfile | log.Ldate | log.Ltime)
mainLogger.Println("=== Logging has begun. === ")
- mainLogger.Println("Given port:", paramPort)
- mainLogger.Println("Given binb_location:", paramBinbLocation)
- mainLogger.Println("Given songs_location:", paramSongsLocation)
- mainLogger.Println("Given redis_location:", paramRedisLocation)
- mainLogger.Println("Given container_name:", paramContainerName)
+ mainLogger.Println("Given port:", *paramPort)
+ mainLogger.Println("Given binb_location:", *paramBinbLocation)
+ mainLogger.Println("Given songs_location:", *paramSongsLocation)
+ mainLogger.Println("Given redis_location:", *paramRedisLocation)
+ mainLogger.Println("Given container_name:", *paramContainerName)
+
+
+ DBwrapper := dbhandling.NewDbWrapper(*paramRedisLocation)
// Setting up the routing
- router := mux.NewRouter()
+ mux := http.NewServeMux()
+ mux.HandleFunc("/api/db_status_update", DBwrapper.GetDBStatus)
+ mux.Handle("/", http.FileServer(http.Dir("./public/")))
+
+ listenPort := ":" + strconv.Itoa(*paramPort)
- router.HandleFunc()
+ fmt.Println("Starting server...")
+ mainLogger.Println("Starting server...")
- http.Handle("/", router)
+ c := cors.New(cors.Options{
+ AllowedOrigins: []string{"http://localhost:8000"},
+ AllowCredentials: true,
+ })
+ handler := c.Handler(mux)
+ err = http.ListenAndServe(listenPort, handler)
+
+ if err != nil {
+ mainLogger.Println("Failed starting the server with:", err)
+ }
}