aboutsummaryrefslogtreecommitdiff
path: root/pkg/api/api0
diff options
context:
space:
mode:
authorpg9182 <96569817+pg9182@users.noreply.github.com>2022-10-13 11:06:41 -0400
committerpg9182 <96569817+pg9182@users.noreply.github.com>2022-10-13 11:06:41 -0400
commit09a7b60b359d7a1b760838d20aa8b8f2b67b983a (patch)
treecf811bee1945fdfb296f4b7842c851df6aa9592c /pkg/api/api0
parent195769230c0809cc186637951855319d1bd13f8e (diff)
downloadAtlas-09a7b60b359d7a1b760838d20aa8b8f2b67b983a.tar.gz
Atlas-09a7b60b359d7a1b760838d20aa8b8f2b67b983a.zip
pkg/api/api0: Minor improvements
Diffstat (limited to 'pkg/api/api0')
-rw-r--r--pkg/api/api0/api.go19
-rw-r--r--pkg/api/api0/playerinfo.go8
2 files changed, 18 insertions, 9 deletions
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
}