From: 2weiEmu Date: Tue, 10 Mar 2026 20:55:25 +0000 (+0100) Subject: added better make settings, getting redis db up, still some short fallings X-Git-Url: https://git.saalbach.dev/?a=commitdiff_plain;h=287a55259071b79871b97f54aec6ee0d37977c93;p=binbsis50-sm.git added better make settings, getting redis db up, still some short fallings --- diff --git a/Makefile b/Makefile index e2c442d..073dee5 100644 --- 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 362d6f1..854f3db 100644 --- 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 7128337..dfa9245 100644 --- 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 7a7c825..5ea134b 100644 --- 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 index 0000000..bf16812 --- /dev/null +++ b/pkg/dbhandling/dbhandling.go @@ -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 index 0000000..f5f3550 --- /dev/null +++ b/public/index.html @@ -0,0 +1,32 @@ + + + + + + + + + + + +
+

Current Status of db:

+ +

List of songs in the database:

+ + +

Which rooms these songs are applied to

+
+ +
+

Room Overview

+
+ +
+

Song Overview

+
+ + + + diff --git a/public/js/index.js b/public/js/index.js new file mode 100644 index 0000000..641c95d --- /dev/null +++ b/public/js/index.js @@ -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 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 index 0000000..82c80a8 --- /dev/null +++ b/testing/dummy_testing_data.py @@ -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()