Add gzip encoding stuff
This commit is contained in:
parent
2ba35c27ae
commit
25c16325b6
64
main.go
64
main.go
|
@ -8,7 +8,6 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -16,12 +15,59 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"compress/gzip"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/sevlyar/go-daemon"
|
"github.com/sevlyar/go-daemon"
|
||||||
gfm "github.com/shurcooL/github_flavored_markdown"
|
gfm "github.com/shurcooL/github_flavored_markdown"
|
||||||
"github.com/shurcooL/github_flavored_markdown/gfmstyle"
|
"github.com/shurcooL/github_flavored_markdown/gfmstyle"
|
||||||
//blackfriday "gopkg.in/russross/blackfriday.v2"
|
//blackfriday "gopkg.in/russross/blackfriday.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// code copied from https://gist.github.com/CJEnright/bc2d8b8dc0c1389a9feeddb110f822d7
|
||||||
|
|
||||||
|
var gzPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
w := gzip.NewWriter(ioutil.Discard)
|
||||||
|
return w
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
type gzipResponseWriter struct {
|
||||||
|
io.Writer
|
||||||
|
http.ResponseWriter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *gzipResponseWriter) WriteHeader(status int) {
|
||||||
|
w.Header().Del("Content-Length")
|
||||||
|
w.ResponseWriter.WriteHeader(status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *gzipResponseWriter) Write(b []byte) (int, error) {
|
||||||
|
return w.Writer.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Gzip(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Encoding", "gzip")
|
||||||
|
|
||||||
|
gz := gzPool.Get().(*gzip.Writer)
|
||||||
|
defer gzPool.Put(gz)
|
||||||
|
|
||||||
|
gz.Reset(w)
|
||||||
|
defer gz.Close()
|
||||||
|
|
||||||
|
next.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
signal = flag.String("s", "", `send signal to the daemon
|
signal = flag.String("s", "", `send signal to the daemon
|
||||||
quit — graceful shutdown
|
quit — graceful shutdown
|
||||||
|
@ -196,13 +242,13 @@ func readWebhookKey() []byte {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
ret := make([]byte, hex.DecodedLen(len(b)))
|
ret := make([]byte, hex.DecodedLen(len(b)))
|
||||||
// skip the ending 0x0a
|
// skip the ending 0x0a
|
||||||
rl, err2 := hex.Decode(ret, b[:len(b)-1])
|
rl, err2 := hex.Decode(ret, b[:len(b)-1])
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
log.Printf("[ERR] unable to decode webhook key! %v %s", b, err2)
|
log.Printf("[ERR] unable to decode webhook key! %v %s", b, err2)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return b[:len(b)-1]
|
return b[:len(b)-1]
|
||||||
|
@ -272,7 +318,7 @@ func startServer(srv *http.Server) {
|
||||||
}
|
}
|
||||||
|
|
||||||
srv.Addr = ":8443"
|
srv.Addr = ":8443"
|
||||||
srv.Handler = serveMux
|
srv.Handler = Gzip(serveMux)
|
||||||
log.Print("starting server")
|
log.Print("starting server")
|
||||||
if !DEBUG {
|
if !DEBUG {
|
||||||
log.Fatal(srv.ListenAndServeTLS("/etc/letsencrypt/live/"+DOMAIN_NAME+"/fullchain.pem",
|
log.Fatal(srv.ListenAndServeTLS("/etc/letsencrypt/live/"+DOMAIN_NAME+"/fullchain.pem",
|
||||||
|
|
Loading…
Reference in New Issue