Added custom cache control headers for static content

This commit is contained in:
Torkel Ödegaard
2015-03-23 18:28:59 -04:00
parent 98c0209976
commit 5f0e7cd52a
6 changed files with 267 additions and 38 deletions
+14 -6
View File
@@ -11,7 +11,6 @@ import (
"path"
"path/filepath"
"strconv"
"time"
"github.com/Unknwon/macaron"
"github.com/codegangsta/cli"
@@ -20,6 +19,7 @@ import (
_ "github.com/macaron-contrib/session/postgres"
"github.com/grafana/grafana/pkg/api"
"github.com/grafana/grafana/pkg/api/static"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/middleware"
@@ -65,14 +65,22 @@ func newMacaron() *macaron.Macaron {
}
func mapStatic(m *macaron.Macaron, dir string, prefix string) {
m.Use(macaron.Static(
headers := func(c *macaron.Context) {
c.Resp.Header().Set("Cache-Control", "public max-age: 3600")
}
if setting.Env == setting.DEV {
headers = func(c *macaron.Context) {
c.Resp.Header().Set("Cache-Control", "max-age: 0")
}
}
m.Use(httpstatic.Static(
path.Join(setting.StaticRootPath, dir),
macaron.StaticOptions{
httpstatic.StaticOptions{
SkipLogging: true,
Prefix: prefix,
Expires: func() string {
return time.Now().UTC().Format(http.TimeFormat)
},
AddHeaders: headers,
},
))
}