cmd/strelaypoolsrv: Serve gzip compressed responses

This commit is contained in:
Jakob Borg 2020-01-15 10:36:21 +01:00
parent d323e9c106
commit 29690502f0

View File

@ -12,6 +12,7 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"mime" "mime"
@ -364,8 +365,8 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
} }
} }
func handleGetRequest(w http.ResponseWriter, r *http.Request) { func handleGetRequest(rw http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8") rw.Header().Set("Content-Type", "application/json; charset=utf-8")
mut.RLock() mut.RLock()
relays := append(permanentRelays, knownRelays...) relays := append(permanentRelays, knownRelays...)
mut.RUnlock() mut.RUnlock()
@ -373,7 +374,15 @@ func handleGetRequest(w http.ResponseWriter, r *http.Request) {
// Shuffle // Shuffle
rand.Shuffle(relays) rand.Shuffle(relays)
json.NewEncoder(w).Encode(map[string][]*relay{ w := io.Writer(rw)
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
rw.Header().Set("Content-Encoding", "gzip")
gw := gzip.NewWriter(rw)
defer gw.Close()
w = gw
}
_ = json.NewEncoder(w).Encode(map[string][]*relay{
"relays": relays, "relays": relays,
}) })
} }