Add cache header
For now we only add it to images/js/css and not for html/json/xml generated pages.
This commit is contained in:
parent
4ace7a687e
commit
bf09d3c0be
3 changed files with 25 additions and 6 deletions
|
@ -26,6 +26,7 @@ const (
|
||||||
NEW_ITEMS_PAGE = 50
|
NEW_ITEMS_PAGE = 50
|
||||||
NUM_NEWS = 10
|
NUM_NEWS = 10
|
||||||
DAYS_NEWS_INDEXPAGE = 15
|
DAYS_NEWS_INDEXPAGE = 15
|
||||||
|
CACHE_MAX_AGE = 1800
|
||||||
|
|
||||||
STORE_PATH = "store/"
|
STORE_PATH = "store/"
|
||||||
TEMPLATE_PATH = "templates/"
|
TEMPLATE_PATH = "templates/"
|
||||||
|
|
1
stats.go
1
stats.go
|
@ -53,6 +53,7 @@ func (sg StatsGatherer) Gather(function func(handler)) func(http.ResponseWriter,
|
||||||
h.w = w
|
h.w = w
|
||||||
h.r = r
|
h.r = r
|
||||||
h.sess = GetSession(r, h.db)
|
h.sess = GetSession(r, h.db)
|
||||||
|
|
||||||
function(h)
|
function(h)
|
||||||
|
|
||||||
sg.channel <- statsRequest{time.Now(), mux.Vars(r), h.sess, r}
|
sg.channel <- statsRequest{time.Now(), mux.Vars(r), h.sess, r}
|
||||||
|
|
29
trantor.go
29
trantor.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
log "github.com/cihub/seelog"
|
log "github.com/cihub/seelog"
|
||||||
|
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -220,11 +221,27 @@ func initRouter(db *database.DB, sg *StatsGatherer) {
|
||||||
r.HandleFunc("/news/edit", sg.Gather(editNewsHandler)).Methods("GET")
|
r.HandleFunc("/news/edit", sg.Gather(editNewsHandler)).Methods("GET")
|
||||||
r.HandleFunc("/news/edit", sg.Gather(postNewsHandler)).Methods("POST")
|
r.HandleFunc("/news/edit", sg.Gather(postNewsHandler)).Methods("POST")
|
||||||
|
|
||||||
h := http.FileServer(http.Dir(IMG_PATH))
|
r.HandleFunc("/img/{img}", fileServer(IMG_PATH, "/img/"))
|
||||||
r.Handle("/img/{img}", http.StripPrefix("/img/", h))
|
r.HandleFunc("/css/{css}", fileServer(CSS_PATH, "/css/"))
|
||||||
h = http.FileServer(http.Dir(CSS_PATH))
|
r.HandleFunc("/js/{js}", fileServer(JS_PATH, "/js/"))
|
||||||
r.Handle("/css/{css}", http.StripPrefix("/css/", h))
|
|
||||||
h = http.FileServer(http.Dir(JS_PATH))
|
|
||||||
r.Handle("/js/{js}", http.StripPrefix("/js/", h))
|
|
||||||
http.Handle("/", r)
|
http.Handle("/", r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fileServer(path string, prefix string) func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// FIXME: is there a cleaner way without handler?
|
||||||
|
h := http.FileServer(http.Dir(path))
|
||||||
|
handler := http.StripPrefix(prefix, h)
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
addCacheControlHeader(w, false)
|
||||||
|
handler.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func addCacheControlHeader(w http.ResponseWriter, private bool) {
|
||||||
|
// FIXME: cache of download and cover don't depends on user login
|
||||||
|
if private {
|
||||||
|
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, private", CACHE_MAX_AGE))
|
||||||
|
} else {
|
||||||
|
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, public", CACHE_MAX_AGE))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Reference in a new issue