Skip to content

Commit

Permalink
added swagger documentation file and changed some handler output to a…
Browse files Browse the repository at this point in the history
…pplication/json returning the right objects
  • Loading branch information
Patrick Hener committed Feb 5, 2021
1 parent 8daae79 commit 406aff5
Show file tree
Hide file tree
Showing 4 changed files with 672 additions and 29 deletions.
2 changes: 2 additions & 0 deletions api/routes/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func websocketSendUpdate(h *ws.Hub) http.HandlerFunc {
Data: []byte("update"),
}
h.Broadcast <- message
w.WriteHeader(http.StatusOK)
}
}

Expand All @@ -26,6 +27,7 @@ func websocketSendRedirect(h *ws.Hub) http.HandlerFunc {
Data: []byte("redirect"),
}
h.Broadcast <- message
w.WriteHeader(http.StatusOK)
}
}

Expand Down
7 changes: 6 additions & 1 deletion game/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func GetAllGames() http.HandlerFunc {
for _, element := range Games {
gamesSlice = append(gamesSlice, element)
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(gamesSlice); err != nil {
logger.Errorf("Error when encoding json: %+v", err)
}
Expand Down Expand Up @@ -215,6 +215,8 @@ func CreateGame(db *sql.DB, h *ws.Hub) http.HandlerFunc {
h.Broadcast <- message

// json encode game as response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(data); err != nil {
logger.Errorf("Error when encoding json: %+v", err)
}
Expand Down Expand Up @@ -245,6 +247,7 @@ func NextPlayer(h *ws.Hub) http.HandlerFunc {
if _, ok := Games[uid]; ok {
game := Games[uid]
game.GameObject.NextPlayer(h)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(game.GameObject); err != nil {
logger.Errorf("Error when encoding json: %+v", err)
Expand Down Expand Up @@ -291,6 +294,7 @@ func InsertThrow(h *ws.Hub) http.HandlerFunc {
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(game.GameObject); err != nil {
logger.Errorf("Error when encoding json: %+v", err)
Expand Down Expand Up @@ -330,6 +334,7 @@ func Rematch(h *ws.Hub) http.HandlerFunc {
http.Error(w, "There was an error triggering", 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(data.GameObject.GetStatusDisplay()); err != nil {
logger.Errorf("Error when encoding json: %+v", err)
Expand Down
37 changes: 9 additions & 28 deletions player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,10 @@ func GetAllPlayer(db *sql.DB) http.HandlerFunc {
return
}

res, err := json.Marshal(&players)
if err != nil {
logger.Errorf("Marshalling players response: %+v", err)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write(res); err != nil {

if err := json.NewEncoder(w).Encode(players); err != nil {
logger.Errorf("Error writing response back to browser: %+v", err)
}
}
Expand Down Expand Up @@ -113,14 +109,9 @@ func GetSpecificPlayer(db *sql.DB) http.HandlerFunc {
return
}

res, err := json.Marshal(&player)
if err != nil {
logger.Errorf("Marshalling player response: %+v", err)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write(res); err != nil {
if err := json.NewEncoder(w).Encode(player); err != nil {
logger.Errorf("Error writing response back to browser: %+v", err)
}
}
Expand Down Expand Up @@ -255,14 +246,9 @@ func AddPlayer(db *sql.DB) http.HandlerFunc {
}
player.UID = strconv.FormatInt(newID, 10)

res, err := json.Marshal(player)
if err != nil {
logger.Errorf("Marshaling JSON response when creating player: %+v", err)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write(res); err != nil {
if err := json.NewEncoder(w).Encode(player); err != nil {
logger.Errorf("Error writing response back to browser: %+v", err)
}
}
Expand Down Expand Up @@ -333,14 +319,9 @@ func UpdatePlayer(db *sql.DB) http.HandlerFunc {
return
}

res, err := json.Marshal(player)
if err != nil {
logger.Errorf("Marshaling JSON response when creating player: %+v", err)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write(res); err != nil {
if err := json.NewEncoder(w).Encode(player); err != nil {
logger.Errorf("Error writing response back to browser: %+v", err)
}
}
Expand Down
Loading

0 comments on commit 406aff5

Please sign in to comment.