From 5d4ea4d2e0be74108cf9dabc1f24dd285ac5ebde Mon Sep 17 00:00:00 2001 From: Azareal Date: Thu, 23 Aug 2018 22:11:52 +1000 Subject: [PATCH] Try this to speed up /uploads/, we might have to revert it if it breaks the caches. --- gen_router.go | 35 +++-------------------------------- router_gen/main.go | 35 +++-------------------------------- 2 files changed, 6 insertions(+), 64 deletions(-) diff --git a/gen_router.go b/gen_router.go index 0f66dde3..3dcf9e6b 100644 --- a/gen_router.go +++ b/gen_router.go @@ -539,32 +539,6 @@ func (w gzipResponseWriter) Write(b []byte) (int, error) { return w.Writer.Write(b) } -type WriterIntercept struct { - w http.ResponseWriter - code int -} - -func NewWriterIntercept(w http.ResponseWriter) *WriterIntercept { - return &WriterIntercept{w:w,code:200} -} - -func (writ *WriterIntercept) Header() http.Header { - return writ.w.Header() -} - -func (writ *WriterIntercept) Write(pieces []byte) (int, error) { - return writ.w.Write(pieces) -} - -func (writ *WriterIntercept) WriteHeader(code int) { - writ.w.WriteHeader(code) - writ.code = code -} - -func (writ *WriterIntercept) GetCode() int { - return writ.code -} - type GenRouter struct { UploadHandler func(http.ResponseWriter, *http.Request) extraRoutes map[string]func(http.ResponseWriter, *http.Request, common.User) common.RouteError @@ -581,12 +555,9 @@ func NewGenRouter(uploads http.Handler) (*GenRouter, error) { return &GenRouter{ UploadHandler: func(w http.ResponseWriter, req *http.Request) { - writ := NewWriterIntercept(w) - http.StripPrefix("/uploads/",uploads).ServeHTTP(writ,req) - if writ.GetCode() == 200 { - w.Header().Set("Cache-Control", "max-age=" + strconv.Itoa(int(common.Day))) - w.Header().Set("Vary", "Accept-Encoding") - } + w.Header().Set("Cache-Control", "max-age=" + strconv.Itoa(int(common.Day))) + w.Header().Set("Vary", "Accept-Encoding") + http.StripPrefix("/uploads/",uploads).ServeHTTP(w,req) }, extraRoutes: make(map[string]func(http.ResponseWriter, *http.Request, common.User) common.RouteError), requestLogger: log.New(f, "", log.LstdFlags), diff --git a/router_gen/main.go b/router_gen/main.go index 25b2da88..72ebb6b8 100644 --- a/router_gen/main.go +++ b/router_gen/main.go @@ -310,32 +310,6 @@ func (w gzipResponseWriter) Write(b []byte) (int, error) { return w.Writer.Write(b) } -type WriterIntercept struct { - w http.ResponseWriter - code int -} - -func NewWriterIntercept(w http.ResponseWriter) *WriterIntercept { - return &WriterIntercept{w:w,code:200} -} - -func (writ *WriterIntercept) Header() http.Header { - return writ.w.Header() -} - -func (writ *WriterIntercept) Write(pieces []byte) (int, error) { - return writ.w.Write(pieces) -} - -func (writ *WriterIntercept) WriteHeader(code int) { - writ.w.WriteHeader(code) - writ.code = code -} - -func (writ *WriterIntercept) GetCode() int { - return writ.code -} - type GenRouter struct { UploadHandler func(http.ResponseWriter, *http.Request) extraRoutes map[string]func(http.ResponseWriter, *http.Request, common.User) common.RouteError @@ -352,12 +326,9 @@ func NewGenRouter(uploads http.Handler) (*GenRouter, error) { return &GenRouter{ UploadHandler: func(w http.ResponseWriter, req *http.Request) { - writ := NewWriterIntercept(w) - http.StripPrefix("/uploads/",uploads).ServeHTTP(writ,req) - if writ.GetCode() == 200 { - w.Header().Set("Cache-Control", "max-age=" + strconv.Itoa(int(common.Day))) - w.Header().Set("Vary", "Accept-Encoding") - } + w.Header().Set("Cache-Control", "max-age=" + strconv.Itoa(int(common.Day))) + w.Header().Set("Vary", "Accept-Encoding") + http.StripPrefix("/uploads/",uploads).ServeHTTP(w,req) }, extraRoutes: make(map[string]func(http.ResponseWriter, *http.Request, common.User) common.RouteError), requestLogger: log.New(f, "", log.LstdFlags),