simplify json response writing
parent
f0c283af42
commit
d70c082aa9
|
@ -19,6 +19,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -48,7 +49,7 @@ func Init(mux *http.ServeMux) error {
|
||||||
mux.HandleFunc("GET /game/classicsessioncount", handleGameClassicSessionCount)
|
mux.HandleFunc("GET /game/classicsessioncount", handleGameClassicSessionCount)
|
||||||
|
|
||||||
// savedata
|
// savedata
|
||||||
mux.HandleFunc("GET /savedata/get", handleSaveData)
|
mux.HandleFunc("GET /savedata/get", getSaveData)
|
||||||
mux.HandleFunc("POST /savedata/update", handleSaveData)
|
mux.HandleFunc("POST /savedata/update", handleSaveData)
|
||||||
mux.HandleFunc("GET /savedata/delete", handleSaveData)
|
mux.HandleFunc("GET /savedata/delete", handleSaveData)
|
||||||
mux.HandleFunc("POST /savedata/clear", 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)
|
log.Printf("%s: %s\n", r.URL.Path, err)
|
||||||
http.Error(w, err.Error(), code)
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ package api
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -58,13 +59,7 @@ func handleAccountInfo(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(response)
|
jsonResponse(w, r, 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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleAccountRegister(w http.ResponseWriter, r *http.Request) {
|
func handleAccountRegister(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -96,13 +91,7 @@ func handleAccountLogin(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(response)
|
jsonResponse(w, r, 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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleAccountChangePW(w http.ResponseWriter, r *http.Request) {
|
func handleAccountChangePW(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -144,24 +133,67 @@ func handleAccountLogout(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// game
|
// game
|
||||||
|
|
||||||
func handleGameTitleStats(w http.ResponseWriter, r *http.Request) {
|
func handleGameTitleStats(w http.ResponseWriter, r *http.Request) {
|
||||||
err := json.NewEncoder(w).Encode(defs.TitleStats{
|
stats := defs.TitleStats{
|
||||||
PlayerCount: playerCount,
|
PlayerCount: playerCount,
|
||||||
BattleCount: battleCount,
|
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) {
|
func handleGameClassicSessionCount(w http.ResponseWriter, r *http.Request) {
|
||||||
_, _ = w.Write([]byte(strconv.Itoa(classicSessionCount)))
|
_, _ = 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) {
|
func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
||||||
token, uuid, err := tokenAndUuidFromRequest(r)
|
token, uuid, err := tokenAndUuidFromRequest(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -312,13 +344,7 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(save)
|
jsonResponse(w, r, 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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CombinedSaveData struct {
|
type CombinedSaveData struct {
|
||||||
|
@ -411,13 +437,7 @@ func handleNewClear(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(newClear)
|
jsonResponse(w, r, 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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// daily
|
// daily
|
||||||
|
@ -459,13 +479,7 @@ func handleDailyRankings(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(w).Encode(rankings)
|
jsonResponse(w, r, 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")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) {
|
func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
Loading…
Reference in New Issue