From d70c082aa91350d8a88f6f0f9dd4a2128c876b80 Mon Sep 17 00:00:00 2001 From: Up Date: Sun, 12 May 2024 20:05:46 +0200 Subject: [PATCH] simplify json response writing --- api/common.go | 12 +++++- api/endpoints.go | 98 +++++++++++++++++++++++++++--------------------- 2 files changed, 67 insertions(+), 43 deletions(-) diff --git a/api/common.go b/api/common.go index b355b92..6b0a88b 100644 --- a/api/common.go +++ b/api/common.go @@ -19,6 +19,7 @@ package api import ( "encoding/base64" + "encoding/json" "fmt" "log" "net/http" @@ -48,7 +49,7 @@ func Init(mux *http.ServeMux) error { mux.HandleFunc("GET /game/classicsessioncount", handleGameClassicSessionCount) // savedata - mux.HandleFunc("GET /savedata/get", handleSaveData) + mux.HandleFunc("GET /savedata/get", getSaveData) mux.HandleFunc("POST /savedata/update", handleSaveData) mux.HandleFunc("GET /savedata/delete", handleSaveData) mux.HandleFunc("POST /savedata/clear", handleSaveData) @@ -104,3 +105,12 @@ func httpError(w http.ResponseWriter, r *http.Request, err error, code int) { log.Printf("%s: %s\n", r.URL.Path, err) http.Error(w, err.Error(), code) } + +func jsonResponse(w http.ResponseWriter, r *http.Request, data any) { + w.Header().Set("Content-Type", "application/json") + err := json.NewEncoder(w).Encode(data) + if err != nil { + httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError) + return + } +} diff --git a/api/endpoints.go b/api/endpoints.go index 5402c3b..ca10ddf 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -20,6 +20,7 @@ package api import ( "database/sql" "encoding/json" + "errors" "fmt" "net/http" "strconv" @@ -58,13 +59,7 @@ func handleAccountInfo(w http.ResponseWriter, r *http.Request) { return } - err = json.NewEncoder(w).Encode(response) - if err != nil { - httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") + jsonResponse(w, r, response) } func handleAccountRegister(w http.ResponseWriter, r *http.Request) { @@ -96,13 +91,7 @@ func handleAccountLogin(w http.ResponseWriter, r *http.Request) { return } - err = json.NewEncoder(w).Encode(response) - if err != nil { - httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") + jsonResponse(w, r, response) } func handleAccountChangePW(w http.ResponseWriter, r *http.Request) { @@ -144,24 +133,67 @@ func handleAccountLogout(w http.ResponseWriter, r *http.Request) { } // game - func handleGameTitleStats(w http.ResponseWriter, r *http.Request) { - err := json.NewEncoder(w).Encode(defs.TitleStats{ + stats := defs.TitleStats{ PlayerCount: playerCount, BattleCount: battleCount, - }) - if err != nil { - httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError) - return } - w.Header().Set("Content-Type", "application/json") + jsonResponse(w, r, stats) } func handleGameClassicSessionCount(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(strconv.Itoa(classicSessionCount))) } +func getSaveData(w http.ResponseWriter, r *http.Request) { + token, uuid, err := tokenAndUuidFromRequest(r) + if err != nil { + httpError(w, r, err, http.StatusBadRequest) + return + } + + datatype := -1 + if r.URL.Query().Has("datatype") { + datatype, err = strconv.Atoi(r.URL.Query().Get("datatype")) + if err != nil { + httpError(w, r, err, http.StatusBadRequest) + return + } + } + + var slot int + if r.URL.Query().Has("slot") { + slot, err = strconv.Atoi(r.URL.Query().Get("slot")) + if err != nil { + httpError(w, r, err, http.StatusBadRequest) + return + } + } + + var save any + if datatype == 0 { + err = db.UpdateActiveSession(uuid, token) + if err != nil { + httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest) + return + } + } + + save, err = savedata.Get(uuid, datatype, slot) + if errors.Is(err, sql.ErrNoRows) { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + + if err != nil { + httpError(w, r, err, http.StatusInternalServerError) + return + } + + jsonResponse(w, r, save) +} + func handleSaveData(w http.ResponseWriter, r *http.Request) { token, uuid, err := tokenAndUuidFromRequest(r) if err != nil { @@ -312,13 +344,7 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) { return } - err = json.NewEncoder(w).Encode(save) - if err != nil { - httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") + jsonResponse(w, r, save) } type CombinedSaveData struct { @@ -411,13 +437,7 @@ func handleNewClear(w http.ResponseWriter, r *http.Request) { return } - err = json.NewEncoder(w).Encode(newClear) - if err != nil { - httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") + jsonResponse(w, r, newClear) } // daily @@ -459,13 +479,7 @@ func handleDailyRankings(w http.ResponseWriter, r *http.Request) { return } - err = json.NewEncoder(w).Encode(rankings) - if err != nil { - httpError(w, r, fmt.Errorf("failed to encode response json: %s", err), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") + jsonResponse(w, r, rankings) } func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) {