diff --git a/server/routes.go b/server/routes.go index d09d972..e5c9d0f 100644 --- a/server/routes.go +++ b/server/routes.go @@ -22,13 +22,14 @@ func (s *Server) Routes() { s.Router.HandleFunc("/generateRoom", s.generateRoom(h)) s.Router.HandleFunc("/listRoomsAndClients", s.listRoomsAndClients(h)) s.Router.HandleFunc("/joinRoom", s.joinRoom(h)) + s.Router.HandleFunc("/checkRoom", s.checkRoom(h)) } func (s *Server) wakeup() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusAccepted) - w.Write([]byte("API is up and running")) + fmt.Fprint(w, "API up and running") } } @@ -44,6 +45,22 @@ func (s *Server) joinRoom(h *socketroom.Hub) http.HandlerFunc { } } +func (s *Server) checkRoom(h *socketroom.Hub) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + roomName := r.URL.Query().Get("room") + defer r.Body.Close() + _, ok := h.Rooms[roomName] + w.Header().Set("Content-Type", "application/json") + if !ok { + w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w, "Room: %q doesn't exist", roomName) + } else { + w.WriteHeader(http.StatusAccepted) + fmt.Fprint(w, "Room exists") + } + } +} + func (s *Server) generateRoom(h *socketroom.Hub) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { pS := r.URL.Query().Get("pointScale")