1 package gameserver 2 3 import ( 4 "bytes" 5 "compress/gzip" 6 _ "embed" // required to embed index.gz 7 "net/http" 8 "time" 9 10 "vimagination.zapto.org/httpencoding" 11 "vimagination.zapto.org/memio" 12 ) 13 14 var ( 15 //go:embed index.gz 16 compressedIndex []byte 17 uncompressedIndex []byte 18 indexUpdatedTime time.Time 19 isGzip = httpencoding.HandlerFunc(func(enc httpencoding.Encoding) bool { return enc == "gzip" }) 20 21 index = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 22 var b *bytes.Reader 23 if httpencoding.HandleEncoding(r, isGzip) { 24 b = bytes.NewReader(compressedIndex) 25 w.Header().Add("Content-Encoding", "gzip") 26 } else { 27 b = bytes.NewReader(uncompressedIndex) 28 } 29 http.ServeContent(w, r, "index.html", indexUpdatedTime, b) 30 }) 31 ) 32 33 func init() { 34 uncompressedIndex = make([]byte, uncompressedSize) 35 r := memio.Buffer(compressedIndex) 36 g, _ := gzip.NewReader(&r) 37 g.Read(uncompressedIndex) 38 indexUpdatedTime = time.Unix(indexUpdated, 0) 39 }