From: 2weiEmu Date: Thu, 21 May 2026 14:34:38 +0000 (+0200) Subject: perhaps jank server restart implemented X-Git-Url: https://git.saalbach.dev/?a=commitdiff_plain;h=6687abf47387a6b3d3fe7b6ce37b6fd92c8671b6;p=binbsis50-sm.git perhaps jank server restart implemented --- diff --git a/Makefile b/Makefile index 283906d..136085f 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ default: . 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="./testing/songs/" -redis_location="localhost:6379" -container_name="" + sudo ./songmanager -port 8000 -binb_location="" -songs_location="./testing/songs/" -redis_location="localhost:6379" -container_name="sis50binb:latest" test: sudo docker stop redis || true diff --git a/main.go b/main.go index cbdb269..b81cb5f 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,7 @@ func main() { mainLogger.Println("Given container_name:", *paramContainerName) - DBwrapper := dbhandling.NewDbWrapper(*paramRedisLocation, *paramSongsLocation) + DBwrapper := dbhandling.NewDbWrapper(*paramRedisLocation, *paramSongsLocation, *paramContainerName, *paramBinbLocation) // Setting up the routing @@ -71,6 +71,8 @@ func main() { mux.HandleFunc("DELETE /api/files/delete/{name}", DBwrapper.FilesDeleteByName) mux.HandleFunc("PUT /api/files/rename/{name}/{newname}", DBwrapper.FilesRenameByName) + mux.HandleFunc("GET /rebootapp", DBwrapper.SongsRestart) + listenPort := ":" + strconv.Itoa(*paramPort) fmt.Println("Starting server...") diff --git a/pkg/dbhandling/dbhandling.go b/pkg/dbhandling/dbhandling.go index f45c69e..1b6e1f1 100644 --- a/pkg/dbhandling/dbhandling.go +++ b/pkg/dbhandling/dbhandling.go @@ -44,9 +44,11 @@ type DBWrapper struct { RedisDb *redis.Client Ctx context.Context PathToSongs string + ImageName string + BinbLocation string } -func NewDbWrapper(redisLocation string, songsLocation string) DBWrapper { +func NewDbWrapper(redisLocation string, songsLocation string, imageName string, binbLocation string) DBWrapper { return DBWrapper { redis.NewClient(&redis.Options{ Addr: redisLocation, @@ -56,6 +58,8 @@ func NewDbWrapper(redisLocation string, songsLocation string) DBWrapper { }), context.Background(), songsLocation, + imageName, + binbLocation, } } diff --git a/pkg/dbhandling/filesapi_test.go b/pkg/dbhandling/filesapi_test.go index bee9ba9..6e13d25 100644 --- a/pkg/dbhandling/filesapi_test.go +++ b/pkg/dbhandling/filesapi_test.go @@ -9,7 +9,7 @@ import ( ) func TestFilesGet(t *testing.T) { - wrapper := dbhandling.NewDbWrapper("localhost:6379", "../../testing/songs") + wrapper := dbhandling.NewDbWrapper("localhost:6379", "../../testing/songs", "", "") t.Run("FilesGet1", func(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/api/files/get", &EmptyReader{}) diff --git a/pkg/dbhandling/roomsapi_test.go b/pkg/dbhandling/roomsapi_test.go index e21ef74..a16ad42 100644 --- a/pkg/dbhandling/roomsapi_test.go +++ b/pkg/dbhandling/roomsapi_test.go @@ -14,7 +14,7 @@ import ( func TestRoomsGet(t *testing.T) { r := httptest.NewRequest("GET", "/api/rooms/get/all", &EmptyReader{}) - wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs", "", "") // subtests t.Run("RoomsGet1", func(t *testing.T) { @@ -34,7 +34,7 @@ func TestRoomsGet(t *testing.T) { } func TestRoomsGetByName(t *testing.T) { - wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs", "", "") // subtests t.Run("RoomsGet1", func(t *testing.T) { @@ -107,7 +107,7 @@ func (a *testAddReader) Read(p []byte) (int, error) { } func TestRoomsAddByNameAndId(t *testing.T) { - wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs", "", "") t.Run("RoomsAddByNameAndId1", func(t *testing.T) { r := httptest.NewRequest(http.MethodPut, "/api/rooms/add/hits", &testAddReader{}) @@ -158,7 +158,7 @@ func (r *testRemoveReader) Read(p []byte) (int, error) { } func TestRoomsRemoveByNameAndId(t *testing.T) { - wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs", "", "") t.Run("RoomsRemoveByNameAndId1", func(t *testing.T) { r := httptest.NewRequest(http.MethodPut, "/api/rooms/remove/hits", &testRemoveReader{}) @@ -194,7 +194,7 @@ func TestRoomsRemoveByNameAndId(t *testing.T) { } func TestRoomsDeleteByName(t *testing.T) { - wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs", "", "") t.Run("RoomsDeleteByName-1", func(t *testing.T) { r := httptest.NewRequest(http.MethodDelete, "/api/rooms/delete/rock", &EmptyReader{}) diff --git a/pkg/dbhandling/songsapi.go b/pkg/dbhandling/songsapi.go index 60e2ee0..657a753 100644 --- a/pkg/dbhandling/songsapi.go +++ b/pkg/dbhandling/songsapi.go @@ -3,6 +3,7 @@ package dbhandling import ( "encoding/json" "net/http" + "os/exec" "strconv" ) @@ -214,3 +215,46 @@ func (dbw *DBWrapper) SongsDeleteById(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + +/** + * Function that will restart the main server that this song manager manges + * so that rooms etc. will all get updated + **/ +func (dbw *DBWrapper) SongsRestart(w http.ResponseWriter, r *http.Request) { + // ok to make this work, we have to do it as follows: + // we have to stop the container, and get rid of it + // then re-run the image. + + // for that we have to know the name of the image (ik it says container name in the thing) + + // sudo docker ps -aqf ancestor=sis50binb:latest // GET THE ID + // sudo docker stop $ID + // sudo docker rm $ID + // ./launch.sh // this might always be present + out, err := exec.Command("/bin/sh", "-c", "sudo docker ps -aqf ancestor=sis50binb:latest").Output() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + id := string(out) + err = exec.Command("/bin/sh", "-c", "sudo docker stop", id).Run() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + err = exec.Command("/bin/sh", "-c", "sudo docker rm", id).Run() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + err = exec.Command("/bin/sh", "-c", "sudo", dbw.BinbLocation + "./launch.sh").Run() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) + +} diff --git a/pkg/dbhandling/songsapi_test.go b/pkg/dbhandling/songsapi_test.go index 7108c42..6ef3ef5 100644 --- a/pkg/dbhandling/songsapi_test.go +++ b/pkg/dbhandling/songsapi_test.go @@ -44,7 +44,7 @@ func (c *CreateReader) Read(p []byte) (int, error) { func TestGetSongs(t *testing.T) { // setup some fake requests r := httptest.NewRequest("GET", "/api/songs/get", &EmptyReader{}) - wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs", "", "") // subtests t.Run("SongsGet1", func(t *testing.T) { @@ -67,7 +67,7 @@ func TestGetSongs(t *testing.T) { func TestSongsCreate(t *testing.T) { // setup fake requests and readers r := httptest.NewRequest("POST", "/api/songs/create", &CreateReader{}) - wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs", "", "") t.Run("SongsCreate1", func(t *testing.T) { recorder := httptest.NewRecorder()