aboutsummaryrefslogtreecommitdiff
path: root/pkg/api/api0
diff options
context:
space:
mode:
authorpg9182 <96569817+pg9182@users.noreply.github.com>2022-10-15 02:37:29 -0400
committerpg9182 <96569817+pg9182@users.noreply.github.com>2022-10-15 02:37:29 -0400
commitec4a20f9fe68273b3784ff0fb6bd1e2a59f31110 (patch)
treea238ffee097cfe9c4e7e06afcd31b5c280bb8b4e /pkg/api/api0
parent62737f1e52146987718fbe36bc8e4fbe14edc25c (diff)
downloadAtlas-ec4a20f9fe68273b3784ff0fb6bd1e2a59f31110.tar.gz
Atlas-ec4a20f9fe68273b3784ff0fb6bd1e2a59f31110.zip
pkg/api/api0: Implement /client/servers
And it's _fast_, even with thousands of servers and hundreds of requests per second.
Diffstat (limited to 'pkg/api/api0')
-rw-r--r--pkg/api/api0/api.go6
-rw-r--r--pkg/api/api0/client.go23
2 files changed, 26 insertions, 3 deletions
diff --git a/pkg/api/api0/api.go b/pkg/api/api0/api.go
index f64203c..b376a14 100644
--- a/pkg/api/api0/api.go
+++ b/pkg/api/api0/api.go
@@ -9,6 +9,7 @@
// - More HTTP methods and features are supported (e.g., HEAD, OPTIONS, Content-Encoding).
// - Website split into a separate handler (set Handler.NotFound to http.HandlerFunc(web.ServeHTTP) for identical behaviour).
// - /accounts/write_persistence returns a error message for easier debugging.
+// - Alive/dead servers can be replaced by a new successful registration from the same ip/port. This eliminates the main cause of the duplicate server error requiring retries, and doesn't add much risk since you need to custom fuckery to start another server when you're already listening on the port.
package api0
import (
@@ -29,6 +30,9 @@ import (
// Handler serves requests for the original master server API.
type Handler struct {
+ // ServerList stores registered servers.
+ ServerList *ServerList
+
// AccountStorage stores accounts. It must be non-nil.
AccountStorage AccountStorage
@@ -71,6 +75,8 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.handleClientOriginAuth(w, r)
case "/client/auth_with_self":
h.handleClientAuthWithSelf(w, r)
+ case "/client/servers":
+ h.handleClientServers(w, r)
case "/accounts/write_persistence":
h.handleAccountsWritePersistence(w, r)
case "/accounts/get_username":
diff --git a/pkg/api/api0/client.go b/pkg/api/api0/client.go
index a5ecbda..d3097b1 100644
--- a/pkg/api/api0/client.go
+++ b/pkg/api/api0/client.go
@@ -438,10 +438,27 @@ func (h *Handler) handleClientAuthWithSelf(w http.ResponseWriter, r *http.Reques
respJSON(w, r, http.StatusOK, obj)
}
+func (h *Handler) handleClientServers(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodOptions && r.Method != http.MethodHead && r.Method != http.MethodGet {
+ http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
+ return
+ }
+
+ w.Header().Set("Cache-Control", "private, no-cache, no-store")
+ w.Header().Set("Expires", "0")
+ w.Header().Set("Pragma", "no-cache")
+
+ if r.Method == http.MethodOptions {
+ w.Header().Set("Allow", "OPTIONS, HEAD, GET")
+ w.WriteHeader(http.StatusNoContent)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/json; charset=utf-8")
+ respMaybeCompress(w, r, http.StatusOK, h.ServerList.csGetJSON())
+}
+
/*
/client/auth_with_server:
POST:
-
- /client/servers:
- GET:
*/