Skip to content

Commit

Permalink
unlock mutex in defer & add additional logging
Browse files Browse the repository at this point in the history
  • Loading branch information
wimspaargaren committed Dec 3, 2024
1 parent e188efc commit 7854fb6
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions cmd/mcvs-stub-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
log.Println("server closed")
}

type handler struct {
Expand Down Expand Up @@ -62,24 +63,22 @@ 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
}

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
}
Expand Down

0 comments on commit 7854fb6

Please sign in to comment.