Use JSON encoders instead of Marshal

pull/4/head
maru 5 months ago
parent 992864b785
commit 01c037a7a2
No known key found for this signature in database
GPG Key ID: 37689350E9CD0F0D

@ -51,19 +51,17 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
info, err := handleAccountInfo(username, uuid) response, err := handleAccountInfo(username, uuid)
if err != nil { if err != nil {
httpError(w, r, err, http.StatusInternalServerError) httpError(w, r, err, http.StatusInternalServerError)
return return
} }
response, err := json.Marshal(info) err = json.NewEncoder(w).Encode(response)
if err != nil { if err != nil {
httpError(w, r, fmt.Errorf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
return return
} }
w.Write(response)
case "/account/register": case "/account/register":
var request AccountRegisterRequest var request AccountRegisterRequest
err := json.NewDecoder(r.Body).Decode(&request) err := json.NewDecoder(r.Body).Decode(&request)
@ -87,19 +85,17 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
token, err := handleAccountLogin(request.Username, request.Password) response, err := handleAccountLogin(request.Username, request.Password)
if err != nil { if err != nil {
httpError(w, r, err, http.StatusInternalServerError) httpError(w, r, err, http.StatusInternalServerError)
return return
} }
response, err := json.Marshal(token) err = json.NewEncoder(w).Encode(response)
if err != nil { if err != nil {
httpError(w, r, fmt.Errorf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
return return
} }
w.Write(response)
case "/account/logout": case "/account/logout":
token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization")) token, err := base64.StdEncoding.DecodeString(r.Header.Get("Authorization"))
if err != nil { if err != nil {
@ -119,16 +115,14 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case "/game/playercount": case "/game/playercount":
w.Write([]byte(strconv.Itoa(playerCount))) w.Write([]byte(strconv.Itoa(playerCount)))
case "/game/titlestats": case "/game/titlestats":
response, err := json.Marshal(&defs.TitleStats{ err := json.NewEncoder(w).Encode(defs.TitleStats{
PlayerCount: playerCount, PlayerCount: playerCount,
BattleCount: battleCount, BattleCount: battleCount,
}) })
if err != nil { if err != nil {
httpError(w, r, fmt.Errorf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
return return
} }
w.Write(response)
case "/game/classicsessioncount": case "/game/classicsessioncount":
w.Write([]byte(strconv.Itoa(classicSessionCount))) w.Write([]byte(strconv.Itoa(classicSessionCount)))
@ -204,14 +198,12 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
response, err := json.Marshal(save) err = json.NewEncoder(w).Encode(save)
if err != nil { if err != nil {
httpError(w, r, fmt.Errorf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
return return
} }
w.Write(response)
// /daily // /daily
case "/daily/seed": case "/daily/seed":
w.Write([]byte(dailyRunSeed)) w.Write([]byte(dailyRunSeed))
@ -246,13 +238,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
response, err := json.Marshal(rankings) err = json.NewEncoder(w).Encode(rankings)
if err != nil { if err != nil {
httpError(w, r, fmt.Errorf("failed to marshal response json: %s", err), http.StatusInternalServerError) httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError)
return return
} }
w.Write(response)
case "/daily/rankingpagecount": case "/daily/rankingpagecount":
var category int var category int
if r.URL.Query().Has("category") { if r.URL.Query().Has("category") {

Loading…
Cancel
Save