Browse Source

Update and evaluate caching headers when returning buddy images.

pull/15/head
Joachim Bauch 12 years ago
parent
commit
03fab9ca97
  1. 14
      src/app/spreed-speakfreely-server/main.go

14
src/app/spreed-speakfreely-server/main.go

@ -22,6 +22,7 @@ package main
import ( import (
"app/spreed-speakfreely-server/sleepy" "app/spreed-speakfreely-server/sleepy"
"bytes"
"flag" "flag"
"fmt" "fmt"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -98,13 +99,16 @@ func makeImageHandler(hub *Hub, expires time.Duration) http.HandlerFunc {
} }
w.Header().Set("Content-Type", image.mimetype) w.Header().Set("Content-Type", image.mimetype)
w.Header().Set("Content-Length", strconv.Itoa(len(image.data))) w.Header().Set("ETag", image.lastChangeId)
w.Header().Set("Date", time.Now().Format(time.RFC822)) age := time.Now().Sub(image.lastChange)
if age >= time.Second {
w.Header().Set("Age", strconv.Itoa(int(age.Seconds())))
}
if expires >= time.Second { if expires >= time.Second {
w.Header().Set("Expires", time.Now().Add(expires).Format(time.RFC1123)) w.Header().Set("Expires", time.Now().Add(expires).Format(time.RFC1123))
w.Header().Set("Cache-Control", "max-age="+strconv.Itoa(int(expires.Seconds()))) w.Header().Set("Cache-Control", "public, no-transform, max-age="+strconv.Itoa(int(expires.Seconds())))
} }
w.Write(image.data) http.ServeContent(w, r, "", image.lastChange, bytes.NewReader(image.data))
} }
} }
@ -319,7 +323,7 @@ func runner(runtime phoenix.Runtime) error {
router := mux.NewRouter() router := mux.NewRouter()
r := router.PathPrefix(basePath).Subrouter().StrictSlash(true) r := router.PathPrefix(basePath).Subrouter().StrictSlash(true)
r.HandleFunc("/", httputils.MakeGzipHandler(mainHandler)) r.HandleFunc("/", httputils.MakeGzipHandler(mainHandler))
r.Handle("/static/img/buddy/{flags}/{imageid}/{idx:.*}", http.StripPrefix(basePath, makeImageHandler(hub, time.Hour))) r.Handle("/static/img/buddy/{flags}/{imageid}/{idx:.*}", http.StripPrefix(basePath, makeImageHandler(hub, time.Duration(24)*time.Hour)))
r.Handle("/static/{path:.*}", http.StripPrefix(basePath, httputils.FileStaticServer(http.Dir(rootFolder)))) r.Handle("/static/{path:.*}", http.StripPrefix(basePath, httputils.FileStaticServer(http.Dir(rootFolder))))
r.Handle("/robots.txt", http.StripPrefix(basePath, http.FileServer(http.Dir(path.Join(rootFolder, "static"))))) r.Handle("/robots.txt", http.StripPrefix(basePath, http.FileServer(http.Dir(path.Join(rootFolder, "static")))))
r.Handle("/favicon.ico", http.StripPrefix(basePath, http.FileServer(http.Dir(path.Join(rootFolder, "static", "img"))))) r.Handle("/favicon.ico", http.StripPrefix(basePath, http.FileServer(http.Dir(path.Join(rootFolder, "static", "img")))))

Loading…
Cancel
Save