Fixed the cache headers on /uploads/ and optimised the route a little.

This commit is contained in:
Azareal 2018-08-24 16:24:27 +10:00
parent 32f3bc453f
commit 4d0d9fd4a5
2 changed files with 14 additions and 42 deletions

View File

@ -540,29 +540,19 @@ func (w gzipResponseWriter) Write(b []byte) (int, error) {
} }
type WriterIntercept struct { type WriterIntercept struct {
w http.ResponseWriter http.ResponseWriter
code int
} }
func NewWriterIntercept(w http.ResponseWriter) *WriterIntercept { func NewWriterIntercept(w http.ResponseWriter) *WriterIntercept {
return &WriterIntercept{w:w,code:200} return &WriterIntercept{w}
}
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) { func (writ *WriterIntercept) WriteHeader(code int) {
writ.w.WriteHeader(code) if code == 200 {
writ.code = code writ.ResponseWriter.Header().Set("Cache-Control", "max-age=" + strconv.Itoa(int(common.Day)))
} writ.ResponseWriter.Header().Set("Vary", "Accept-Encoding")
}
func (writ *WriterIntercept) GetCode() int { writ.ResponseWriter.WriteHeader(code)
return writ.code
} }
type GenRouter struct { type GenRouter struct {
@ -583,10 +573,6 @@ func NewGenRouter(uploads http.Handler) (*GenRouter, error) {
UploadHandler: func(w http.ResponseWriter, req *http.Request) { UploadHandler: func(w http.ResponseWriter, req *http.Request) {
writ := NewWriterIntercept(w) writ := NewWriterIntercept(w)
http.StripPrefix("/uploads/",uploads).ServeHTTP(writ,req) 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), extraRoutes: make(map[string]func(http.ResponseWriter, *http.Request, common.User) common.RouteError),
requestLogger: log.New(f, "", log.LstdFlags), requestLogger: log.New(f, "", log.LstdFlags),

View File

@ -311,29 +311,19 @@ func (w gzipResponseWriter) Write(b []byte) (int, error) {
} }
type WriterIntercept struct { type WriterIntercept struct {
w http.ResponseWriter http.ResponseWriter
code int
} }
func NewWriterIntercept(w http.ResponseWriter) *WriterIntercept { func NewWriterIntercept(w http.ResponseWriter) *WriterIntercept {
return &WriterIntercept{w:w,code:200} return &WriterIntercept{w}
}
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) { func (writ *WriterIntercept) WriteHeader(code int) {
writ.w.WriteHeader(code) if code == 200 {
writ.code = code writ.ResponseWriter.Header().Set("Cache-Control", "max-age=" + strconv.Itoa(int(common.Day)))
} writ.ResponseWriter.Header().Set("Vary", "Accept-Encoding")
}
func (writ *WriterIntercept) GetCode() int { writ.ResponseWriter.WriteHeader(code)
return writ.code
} }
type GenRouter struct { type GenRouter struct {
@ -354,10 +344,6 @@ func NewGenRouter(uploads http.Handler) (*GenRouter, error) {
UploadHandler: func(w http.ResponseWriter, req *http.Request) { UploadHandler: func(w http.ResponseWriter, req *http.Request) {
writ := NewWriterIntercept(w) writ := NewWriterIntercept(w)
http.StripPrefix("/uploads/",uploads).ServeHTTP(writ,req) 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), extraRoutes: make(map[string]func(http.ResponseWriter, *http.Request, common.User) common.RouteError),
requestLogger: log.New(f, "", log.LstdFlags), requestLogger: log.New(f, "", log.LstdFlags),