fixed some httpstatu.es

This commit is contained in:
Stephen McQuay 2013-11-15 08:54:52 -08:00
parent 50868809bd
commit f8fb523a76

View File

@ -41,7 +41,6 @@ func startGame(w http.ResponseWriter, req *http.Request) {
}{}
err = json.Unmarshal(body, &cfg)
if err != nil {
NewFailure(err.Error())
if err := json.NewEncoder(w).Encode(NewFailure(err.Error())); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
@ -108,16 +107,21 @@ func listGames(w http.ResponseWriter, req *http.Request) {
}
func gameStats(w http.ResponseWriter, req *http.Request) {
// TODO: wrap this up in something similar to the JsonHandler to verify the
// url? Look at gorilla routing?
key, err := getGameId(req.URL.Path)
if err != nil {
http.Error(w, "improperly formed url", http.StatusBadRequest)
b, _ := json.Marshal(NewFailure(err.Error()))
http.Error(w, string(b), http.StatusBadRequest)
return
}
log.Printf("requested stats for game: %s", key)
games.RLock()
g, ok := games.m[key]
games.RUnlock()
if !ok {
http.NotFound(w, req)
b, _ := json.Marshal(NewFailure("game not found"))
http.Error(w, string(b), http.StatusNotFound)
return
}
g.winners.RLock()
@ -130,7 +134,9 @@ func gameStats(w http.ResponseWriter, req *http.Request) {
func stopGame(w http.ResponseWriter, req *http.Request) {
key, err := getGameId(req.URL.Path)
if err != nil {
http.Error(w, "improperly formed url", http.StatusBadRequest)
b, _ := json.Marshal(NewFailure(err.Error()))
http.Error(w, string(b), http.StatusBadRequest)
return
}
games.Lock()
g, ok := games.m[key]