From 7854fb6dfb808071a2c2be48f67162664c587344 Mon Sep 17 00:00:00 2001 From: Wim Spaargaren Date: Tue, 3 Dec 2024 11:20:12 +0100 Subject: [PATCH] unlock mutex in defer & add additional logging --- cmd/mcvs-stub-server/main.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cmd/mcvs-stub-server/main.go b/cmd/mcvs-stub-server/main.go index 7e75827..6b938c6 100644 --- a/cmd/mcvs-stub-server/main.go +++ b/cmd/mcvs-stub-server/main.go @@ -16,6 +16,7 @@ func main() { if err != nil { log.Fatal(err) } + log.Println("server closed") } type handler struct { @@ -62,17 +63,14 @@ func (h *handler) configure(w http.ResponseWriter, r *http.Request) { return } h.mu.Lock() + defer h.mu.Unlock() h.endpoints[request.Path] = request.Response - h.mu.Unlock() w.WriteHeader(http.StatusOK) } func (h *handler) catchAll(w http.ResponseWriter, r *http.Request) { - h.mu.RLock() response, exists := h.endpoints[r.URL.Path] - h.mu.RUnlock() - if !exists { http.NotFound(w, r) return @@ -80,6 +78,7 @@ func (h *handler) catchAll(w http.ResponseWriter, r *http.Request) { b, err := json.Marshal(response) if err != nil { + log.Default().Println("Failed to marshal response:", err) http.Error(w, "Internal server error", http.StatusInternalServerError) return }