From 09a7b60b359d7a1b760838d20aa8b8f2b67b983a Mon Sep 17 00:00:00 2001 From: pg9182 <96569817+pg9182@users.noreply.github.com> Date: Thu, 13 Oct 2022 11:06:41 -0400 Subject: pkg/api/api0: Minor improvements --- pkg/api/api0/api.go | 19 ++++++++++++++----- pkg/api/api0/playerinfo.go | 8 ++++---- 2 files changed, 18 insertions(+), 9 deletions(-) (limited to 'pkg/api/api0') diff --git a/pkg/api/api0/api.go b/pkg/api/api0/api.go index e6c6550..6e1cbd8 100644 --- a/pkg/api/api0/api.go +++ b/pkg/api/api0/api.go @@ -19,19 +19,25 @@ import ( "strings" ) +// Handler serves requests for the original master server API. type Handler struct { + // PdataStorage stores player data. It must be non-nil. PdataStorage PdataStorage - NotFound http.Handler + + // NotFound handles requests not handled by this Handler. + NotFound http.Handler } +// ServeHTTP routes requests to Handler. func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set("Server", "Atlas") - switch { - case strings.HasPrefix(r.URL.Path, "/player/"): - // TODO: rate limit - h.handlePlayer(w, r) + switch r.URL.Path { default: + if strings.HasPrefix(r.URL.Path, "/player/") { + // TODO: rate limit + h.handlePlayer(w, r) + } if h.NotFound == nil { http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) } else { @@ -40,6 +46,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +// respJSON writes the JSON encoding of obj with the provided response status. func respJSON(w http.ResponseWriter, status int, obj any) { buf, err := json.Marshal(obj) if err != nil { @@ -51,6 +58,8 @@ func respJSON(w http.ResponseWriter, status int, obj any) { w.Write(buf) } +// respMaybeCompress writes buf with the provided response status, compressing +// it with gzip if the client supports it and the result is smaller. func respMaybeCompress(w http.ResponseWriter, r *http.Request, status int, buf []byte) { for _, e := range strings.Split(r.Header.Get("Accept-Encoding"), ",") { if t, _, _ := strings.Cut(e, ";"); strings.TrimSpace(t) == "gzip" { diff --git a/pkg/api/api0/playerinfo.go b/pkg/api/api0/playerinfo.go index f7ad33c..94ff117 100644 --- a/pkg/api/api0/playerinfo.go +++ b/pkg/api/api0/playerinfo.go @@ -98,7 +98,7 @@ func (h *Handler) handlePlayer(w http.ResponseWriter, r *http.Request) { respJSON(w, http.StatusInternalServerError, map[string]any{ "success": false, "error": ErrorCode_INTERNAL_SERVER_ERROR, - "msg": fmt.Sprintf("%s: failed to read pdata hash from storage", ErrorCode_INTERNAL_SERVER_ERROR), + "msg": fmt.Sprintf("%s: failed to read pdata hash from storage", ErrorCode_INTERNAL_SERVER_ERROR.Message()), }) return } @@ -123,7 +123,7 @@ func (h *Handler) handlePlayer(w http.ResponseWriter, r *http.Request) { respJSON(w, http.StatusInternalServerError, map[string]any{ "success": false, "error": ErrorCode_INTERNAL_SERVER_ERROR, - "msg": fmt.Sprintf("%s: failed to read pdata hash from storage", ErrorCode_INTERNAL_SERVER_ERROR), + "msg": fmt.Sprintf("%s: failed to read pdata hash from storage", ErrorCode_INTERNAL_SERVER_ERROR.Message()), }) return } @@ -148,7 +148,7 @@ func (h *Handler) handlePlayer(w http.ResponseWriter, r *http.Request) { respJSON(w, http.StatusInternalServerError, map[string]any{ "success": false, "error": ErrorCode_INTERNAL_SERVER_ERROR, - "msg": fmt.Sprintf("%s: failed to parse pdata from storage", ErrorCode_INTERNAL_SERVER_ERROR), + "msg": fmt.Sprintf("%s: failed to parse pdata from storage", ErrorCode_INTERNAL_SERVER_ERROR.Message()), }) return } @@ -163,7 +163,7 @@ func (h *Handler) handlePlayer(w http.ResponseWriter, r *http.Request) { respJSON(w, http.StatusInternalServerError, map[string]any{ "success": false, "error": ErrorCode_INTERNAL_SERVER_ERROR, - "msg": fmt.Sprintf("%s: failed to encode pdata as json", ErrorCode_INTERNAL_SERVER_ERROR), + "msg": fmt.Sprintf("%s: failed to encode pdata as json", ErrorCode_INTERNAL_SERVER_ERROR.Message()), }) return } -- cgit v1.2.3