From 4d0d9fd4a54662ebf179c63a2a7650f73f91f2e1 Mon Sep 17 00:00:00 2001 From: Azareal Date: Fri, 24 Aug 2018 16:24:27 +1000 Subject: [PATCH] Fixed the cache headers on /uploads/ and optimised the route a little. --- gen_router.go | 28 +++++++--------------------- router_gen/main.go | 28 +++++++--------------------- 2 files changed, 14 insertions(+), 42 deletions(-) diff --git a/gen_router.go b/gen_router.go index 0f66dde3..abdfe97a 100644 --- a/gen_router.go +++ b/gen_router.go @@ -540,29 +540,19 @@ func (w gzipResponseWriter) Write(b []byte) (int, error) { } type WriterIntercept struct { - w http.ResponseWriter - code int + http.ResponseWriter } 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) + return &WriterIntercept{w} } func (writ *WriterIntercept) WriteHeader(code int) { - writ.w.WriteHeader(code) - writ.code = code -} - -func (writ *WriterIntercept) GetCode() int { - return writ.code + if code == 200 { + writ.ResponseWriter.Header().Set("Cache-Control", "max-age=" + strconv.Itoa(int(common.Day))) + writ.ResponseWriter.Header().Set("Vary", "Accept-Encoding") + } + writ.ResponseWriter.WriteHeader(code) } type GenRouter struct { @@ -583,10 +573,6 @@ func NewGenRouter(uploads http.Handler) (*GenRouter, error) { 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") - } }, 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..40dd0e1a 100644 --- a/router_gen/main.go +++ b/router_gen/main.go @@ -311,29 +311,19 @@ func (w gzipResponseWriter) Write(b []byte) (int, error) { } type WriterIntercept struct { - w http.ResponseWriter - code int + http.ResponseWriter } 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) + return &WriterIntercept{w} } func (writ *WriterIntercept) WriteHeader(code int) { - writ.w.WriteHeader(code) - writ.code = code -} - -func (writ *WriterIntercept) GetCode() int { - return writ.code + if code == 200 { + writ.ResponseWriter.Header().Set("Cache-Control", "max-age=" + strconv.Itoa(int(common.Day))) + writ.ResponseWriter.Header().Set("Vary", "Accept-Encoding") + } + writ.ResponseWriter.WriteHeader(code) } type GenRouter struct { @@ -354,10 +344,6 @@ func NewGenRouter(uploads http.Handler) (*GenRouter, error) { 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") - } }, extraRoutes: make(map[string]func(http.ResponseWriter, *http.Request, common.User) common.RouteError), requestLogger: log.New(f, "", log.LstdFlags),