From b6604153d18453db4afbf4c7e861ec546c77e1bd Mon Sep 17 00:00:00 2001 From: 2weiEmu Date: Mon, 13 Apr 2026 10:46:15 +0200 Subject: [PATCH] updated: mux has method filtering built-in --- main.go | 30 +++++++++++++++--------------- pkg/dbhandling/filesapi.go | 24 ------------------------ pkg/dbhandling/roomsapi.go | 20 -------------------- pkg/dbhandling/songsapi.go | 26 -------------------------- pkg/dbhandling/songsapi_test.go | 11 +++++++++++ testing/dummy_testing_data.py | 2 +- 6 files changed, 27 insertions(+), 86 deletions(-) create mode 100644 pkg/dbhandling/songsapi_test.go diff --git a/main.go b/main.go index 8fedd66..6f5b502 100644 --- a/main.go +++ b/main.go @@ -52,25 +52,25 @@ func main() { mux.Handle("/", http.FileServer(http.Dir("./public/"))) // api/songs/ - api related to raw songs - mux.HandleFunc("/api/songs/get", DBwrapper.SongsGet) - mux.HandleFunc("/api/songs/create", DBwrapper.SongsCreate) - mux.HandleFunc("/api/songs/update/{id}", DBwrapper.SongsUpdateById) - mux.HandleFunc("/api/songs/delete/{id}", DBwrapper.SongsDeleteById) + mux.HandleFunc("GET /api/songs/get", DBwrapper.SongsGet) + mux.HandleFunc("POST /api/songs/create", DBwrapper.SongsCreate) + mux.HandleFunc("PUT /api/songs/update/{id}", DBwrapper.SongsUpdateById) + mux.HandleFunc("DELETE /api/songs/delete/{id}", DBwrapper.SongsDeleteById) // api/rooms - api related to managaing rooms - mux.HandleFunc("/api/rooms/get", DBwrapper.RoomsGet) - mux.HandleFunc("/api/rooms/create/", DBwrapper.RoomsCreate) - mux.HandleFunc("/api/rooms/add/{name}/{id}", DBwrapper.RoomsAddByNameAndId) - mux.HandleFunc("/api/rooms/remove/{name}/{id}", DBwrapper.RoomsRemoveByNameAndId) - mux.HandleFunc("/api/rooms/delete/{name}", DBwrapper.RoomsDeleteByName) + mux.HandleFunc("GET /api/rooms/get", DBwrapper.RoomsGet) + mux.HandleFunc("POST /api/rooms/create/", DBwrapper.RoomsCreate) + mux.HandleFunc("PUT /api/rooms/add/{name}/{id}", DBwrapper.RoomsAddByNameAndId) + mux.HandleFunc("PUT /api/rooms/remove/{name}/{id}", DBwrapper.RoomsRemoveByNameAndId) + mux.HandleFunc("DELETE /api/rooms/delete/{name}", DBwrapper.RoomsDeleteByName) // api/files - api related to song files - mux.HandleFunc("/api/files/get", DBwrapper.FilesGet) - mux.HandleFunc("/api/files/createWithURL", DBwrapper.FilesCreateWithURL) - mux.HandleFunc("/api/files/createWithUpload", DBwrapper.FilesCreateWithUpload) - mux.HandleFunc("/api/files/delete/{id}", DBwrapper.FilesDeleteById) - mux.HandleFunc("/api/files/rename/{id}", DBwrapper.FilesRenameById) - mux.HandleFunc("/api/files/replace/{id}", DBwrapper.FilesReplaceById) + mux.HandleFunc("GET /api/files/get", DBwrapper.FilesGet) + mux.HandleFunc("POST /api/files/createWithURL", DBwrapper.FilesCreateWithURL) + mux.HandleFunc("POST /api/files/createWithUpload", DBwrapper.FilesCreateWithUpload) + mux.HandleFunc("DELETE /api/files/delete/{id}", DBwrapper.FilesDeleteById) + mux.HandleFunc("PUT /api/files/rename/{id}", DBwrapper.FilesRenameById) + mux.HandleFunc("PUT /api/files/replace/{id}", DBwrapper.FilesReplaceById) listenPort := ":" + strconv.Itoa(*paramPort) diff --git a/pkg/dbhandling/filesapi.go b/pkg/dbhandling/filesapi.go index 3f1278b..c79d1b8 100644 --- a/pkg/dbhandling/filesapi.go +++ b/pkg/dbhandling/filesapi.go @@ -6,58 +6,34 @@ import "net/http" * **/ func (dbw *DBWrapper) FilesGet(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } /** TODO * **/ func (dbw *DBWrapper) FilesCreateWithURL(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } /** TODO * **/ func (dbw *DBWrapper) FilesCreateWithUpload(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } /** TODO * **/ func (dbw *DBWrapper) FilesDeleteById(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } /** TODO * **/ func (dbw *DBWrapper) FilesRenameById(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } /** TODO * **/ func (dbw *DBWrapper) FilesReplaceById(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } diff --git a/pkg/dbhandling/roomsapi.go b/pkg/dbhandling/roomsapi.go index 0f38929..64af91a 100644 --- a/pkg/dbhandling/roomsapi.go +++ b/pkg/dbhandling/roomsapi.go @@ -6,48 +6,28 @@ import "net/http" * **/ func (dbw *DBWrapper) RoomsGet(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } /** TODO * **/ func (dbw *DBWrapper) RoomsCreate(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } /** TODO * **/ func (dbw *DBWrapper) RoomsAddByNameAndId(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } /** TODO * **/ func (dbw *DBWrapper) RoomsRemoveByNameAndId(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } /** TODO * **/ func (dbw *DBWrapper) RoomsDeleteByName(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } diff --git a/pkg/dbhandling/songsapi.go b/pkg/dbhandling/songsapi.go index 98e7091..58d794c 100644 --- a/pkg/dbhandling/songsapi.go +++ b/pkg/dbhandling/songsapi.go @@ -6,50 +6,24 @@ import "net/http" * Get all songs, and all their assosciated information from the db **/ func (dbw *DBWrapper) SongsGet(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } - // Here we should return an entire list of songs -} -/** TODO - * Create a new song in the database (only in the main list don't add to anything) - **/ -func (dbw *DBWrapper) SongsCreate(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } /** TODO * Create a new song in the database (only in the main list don't add to anything) **/ func (dbw *DBWrapper) SongsCreate(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } /** TODO * Create a new song in the database (only in the main list don't add to anything) **/ func (dbw *DBWrapper) SongsUpdateById(w http.ResponseWriter, r *http.Request) { - if r.Method != "PUT" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } /** TODO * Create a new song in the database (only in the main list don't add to anything) **/ func (dbw *DBWrapper) SongsDeleteById(w http.ResponseWriter, r *http.Request) { - if r.Method != "DELETE" { - w.WriteHeader(http.StatusMethodNotAllowed) - return - } } diff --git a/pkg/dbhandling/songsapi_test.go b/pkg/dbhandling/songsapi_test.go new file mode 100644 index 0000000..524e3ca --- /dev/null +++ b/pkg/dbhandling/songsapi_test.go @@ -0,0 +1,11 @@ +package dbhandling_test + +import "testing" + +func TestGetSongs(t *testing.T) { + // setup some fake requests + + // subtests + + // get answers to those requests +} diff --git a/testing/dummy_testing_data.py b/testing/dummy_testing_data.py index 2bde741..6d41e51 100644 --- a/testing/dummy_testing_data.py +++ b/testing/dummy_testing_data.py @@ -7,7 +7,7 @@ def main(): with open("song-map.csv", "r") as mapcsv: all = mapcsv.readlines() - for i in range(0, 100): + for i in range(0, 10): line = all[i].split(",") r.hmset(f"song:{i}", { "artistName": line[0], -- 2.54.0