]> git.example.dev Git - binbsis50-sm.git/commitdiff
added better make settings, getting redis db up, still some short fallings
author2weiEmu <saalbach.robert@outlook.de>
Tue, 10 Mar 2026 20:55:25 +0000 (21:55 +0100)
committer2weiEmu <saalbach.robert@outlook.de>
Tue, 10 Mar 2026 20:55:25 +0000 (21:55 +0100)
Makefile
go.mod
go.sum
main.go
pkg/dbhandling/dbhandling.go [new file with mode: 0644]
public/index.html [new file with mode: 0644]
public/js/index.js [new file with mode: 0644]
songmanager [new file with mode: 0755]
testing/dummy_testing_data.py [new file with mode: 0644]

index e2c442d0d27b902eb74aa6d67fa4097239248b4b..073dee5c3a531b767c2bb6f12ff25266bbc21166 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,12 @@
 
 
 default: .
-       go run .
+       # first we must setup the test database
+       sudo docker stop redis || true
+       sudo docker rm redis || true
+       sudo docker run -d --name redis -p 6379:6379 redis:latest
+       
+       python3 ./testing/dummy_testing_data.py
+       # now we can run the main app with the right arguments
+       go build .
+       ./songmanager -port 8000 -binb_location="" -songs_location="" -redis_location="localhost:6379" -container_name=""
diff --git a/go.mod b/go.mod
index 362d6f1732c5d7f54b74e63a8b3b7faeb5b87ebc..854f3db70840582df956398f369e2a4c0d80f977 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -3,3 +3,11 @@ module songmanager
 go 1.25.0
 
 require github.com/gorilla/mux v1.8.1
+
+require (
+       github.com/cespare/xxhash/v2 v2.3.0 // indirect
+       github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
+       github.com/redis/go-redis/v9 v9.18.0 // indirect
+       github.com/rs/cors v1.11.1 // indirect
+       go.uber.org/atomic v1.11.0 // indirect
+)
diff --git a/go.sum b/go.sum
index 7128337432e7fb0f28e676baf647997e1fcbfa5c..dfa9245a378e940c0bfde173b10f957f8772d631 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -1,2 +1,12 @@
+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=
diff --git a/main.go b/main.go
index 7a7c82565d6866182ee690b86c9776bc888db3cf..5ea134b95a8368bc100d4b654be2fd0163882c4d 100644 (file)
--- a/main.go
+++ b/main.go
@@ -6,43 +6,61 @@ import (
        "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)
+       }
 }
diff --git a/pkg/dbhandling/dbhandling.go b/pkg/dbhandling/dbhandling.go
new file mode 100644 (file)
index 0000000..bf16812
--- /dev/null
@@ -0,0 +1,35 @@
+package dbhandling
+
+import (
+       "context"
+       "fmt"
+       "net/http"
+
+       "github.com/redis/go-redis/v9"
+)
+
+type DBWrapper struct {
+       RedisDb *redis.Client
+       ctx context.Context
+}
+
+func NewDbWrapper(redisLocation string) DBWrapper {
+       return DBWrapper {
+               redis.NewClient(&redis.Options{
+                       Addr: redisLocation,
+                       Password: "",
+                       DB: 0,
+                       Protocol: 2,
+               }),
+               context.Background(),
+       }
+}
+
+func (dbw *DBWrapper) GetDBStatus(w http.ResponseWriter, r *http.Request) {
+       if r.Method != "GET" {
+               w.WriteHeader(http.StatusMethodNotAllowed)
+               return
+       }
+
+       fmt.Println(dbw.RedisDb.Keys(dbw.ctx, "song"))
+}
diff --git a/public/index.html b/public/index.html
new file mode 100644 (file)
index 0000000..f5f3550
--- /dev/null
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+
+
+<html>
+       <head>
+               <link href="/css/index.css" rel="stylesheet" type="text/css">
+               <meta name="viewport" content="width=device-width, initial-scale=1.0">
+               <link rel="icon" type="image/x-icon" href="/img/favicon.ico">
+       </head>
+
+       <body>
+               <div>
+                       <h1>Current Status of db:</h1>
+                       <button type="button" onclick="get_updated_status()">Get Status</button>
+                       <h2>List of songs in the database:</h2>
+                       <ul id="db_status_song_list">
+                       </ul>
+
+                       <h2>Which rooms these songs are applied to</h2>
+               </div>
+
+               <div>
+                       <h1>Room Overview</h1>
+               </div>
+
+               <div>
+                       <h1>Song Overview</h1>
+               </div>
+       </body>
+       <script type="text/javascript" src="/js/index.js"></script>
+</html>
+
diff --git a/public/js/index.js b/public/js/index.js
new file mode 100644 (file)
index 0000000..641c95d
--- /dev/null
@@ -0,0 +1,12 @@
+function get_updated_status() {
+       console.log("updating database status...")
+
+       fetch("http://localhost:8000/api/db_status_update").then((response) => {
+               console.log(response)
+       })
+}
+
+window.onload = () => {
+       get_updated_status()
+}
+
diff --git a/songmanager b/songmanager
new file mode 100755 (executable)
index 0000000..a163903
Binary files /dev/null and b/songmanager differ
diff --git a/testing/dummy_testing_data.py b/testing/dummy_testing_data.py
new file mode 100644 (file)
index 0000000..82c80a8
--- /dev/null
@@ -0,0 +1,17 @@
+import redis
+
+def main():
+    r = redis.Redis(host="localhost", port="6379", decode_responses=True)
+
+    for i in range(0, 100):
+        r.hmset(f"song:{i}", {
+            "artistNames": "firstartist, secondartist",
+            "songNames": "firstsongname, secondsongname"
+        })
+        r.zadd("hits", { i: i } )
+
+
+
+
+if __name__ == "__main__":
+    main()