Add caching for static web assets

This commit is contained in:
Benedikt Kulmann
2020-11-16 23:22:56 +01:00
parent aaf9029a3e
commit 9c6dac8328
10 changed files with 50 additions and 5 deletions
+1
View File
@@ -29,6 +29,7 @@ type HTTP struct {
Addr string
Namespace string
Root string
CacheTTL int
}
// GRPC defines the available grpc configuration.
+7
View File
@@ -57,6 +57,13 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
EnvVars: []string{"ACCOUNTS_HTTP_ROOT"},
Destination: &cfg.HTTP.Root,
},
&cli.IntFlag{
Name: "http-cache-ttl",
Value: 604800, // 7 days
Usage: "Set the static assets caching duration in seconds",
EnvVars: []string{"ACCOUNTS_CACHE_TTL"},
Destination: &cfg.HTTP.CacheTTL,
},
&cli.StringFlag{
Name: "grpc-namespace",
Value: "com.owncloud.api",
+1
View File
@@ -52,6 +52,7 @@ func Server(opts ...Option) http.Service {
assets.Logger(options.Logger),
assets.Config(options.Config),
),
options.Config.HTTP.CacheTTL,
))
mux.Route(options.Config.HTTP.Root, func(r chi.Router) {
+1
View File
@@ -20,6 +20,7 @@ type HTTP struct {
Addr string
Root string
Namespace string
CacheTTL int
}
// Tracing defines the available tracing configuration.
+7
View File
@@ -136,6 +136,13 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
EnvVars: []string{"PHOENIX_NAMESPACE"},
Destination: &cfg.HTTP.Namespace,
},
&cli.IntFlag{
Name: "http-cache-ttl",
Value: 604800, // 7 days
Usage: "Set the static assets caching duration in seconds",
EnvVars: []string{"PHOENIX_CACHE_TTL"},
Destination: &cfg.HTTP.CacheTTL,
},
&cli.StringFlag{
Name: "asset-path",
Value: "",
+14 -4
View File
@@ -2,10 +2,13 @@ package svc
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/go-chi/chi"
"github.com/owncloud/ocis/ocis-phoenix/pkg/assets"
@@ -39,7 +42,7 @@ func NewService(opts ...Option) Service {
m.Route(options.Config.HTTP.Root, func(r chi.Router) {
r.Get("/config.json", svc.Config)
r.Mount("/", svc.Static())
r.Mount("/", svc.Static(options.Config.HTTP.CacheTTL))
})
return svc
@@ -124,7 +127,7 @@ func (p Phoenix) Config(w http.ResponseWriter, r *http.Request) {
}
// Static simply serves all static files.
func (p Phoenix) Static() http.HandlerFunc {
func (p Phoenix) Static(ttl int) http.HandlerFunc {
rootWithSlash := p.config.HTTP.Root
if !strings.HasSuffix(rootWithSlash, "/") {
@@ -141,7 +144,10 @@ func (p Phoenix) Static() http.HandlerFunc {
),
)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// we don't have a last modification date of the static assets, so we use the service start date
lastModified := time.Now().UTC().Format(http.TimeFormat)
return func(w http.ResponseWriter, r *http.Request) {
if rootWithSlash != "/" && r.URL.Path == p.config.HTTP.Root {
http.Redirect(
w,
@@ -162,6 +168,10 @@ func (p Phoenix) Static() http.HandlerFunc {
return
}
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%s", strconv.Itoa(ttl)))
w.Header().Set("Last-Modified", lastModified)
w.Header().Del("Expires")
static.ServeHTTP(w, r)
})
}
}
+10 -1
View File
@@ -1,13 +1,16 @@
package middleware
import (
"fmt"
"net/http"
"path"
"strconv"
"strings"
"time"
)
// Static is a middleware that serves static assets.
func Static(root string, fs http.FileSystem) func(http.Handler) http.Handler {
func Static(root string, fs http.FileSystem, ttl int) func(http.Handler) http.Handler {
if !strings.HasSuffix(root, "/") {
root = root + "/"
}
@@ -19,6 +22,9 @@ func Static(root string, fs http.FileSystem) func(http.Handler) http.Handler {
),
)
// we don't have a last modification date of the static assets, so we use the service start date
lastModified := time.Now().UTC().Format(http.TimeFormat)
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, path.Join(root, "api")) {
@@ -27,6 +33,9 @@ func Static(root string, fs http.FileSystem) func(http.Handler) http.Handler {
if strings.HasSuffix(r.URL.Path, "/") {
http.NotFound(w, r)
} else {
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%s", strconv.Itoa(ttl)))
w.Header().Set("Last-Modified", lastModified)
w.Header().Del("Expires")
static.ServeHTTP(w, r)
}
}
+1
View File
@@ -20,6 +20,7 @@ type HTTP struct {
Addr string
Namespace string
Root string
CacheTTL int
}
// GRPC defines the available grpc configuration.
+7
View File
@@ -136,6 +136,13 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
EnvVars: []string{"SETTINGS_HTTP_ROOT"},
Destination: &cfg.HTTP.Root,
},
&cli.IntFlag{
Name: "http-cache-ttl",
Value: 604800, // 7 days
Usage: "Set the static assets caching duration in seconds",
EnvVars: []string{"SETTINGS_CACHE_TTL"},
Destination: &cfg.HTTP.CacheTTL,
},
&cli.StringFlag{
Name: "grpc-addr",
Value: "0.0.0.0:9191",
+1
View File
@@ -60,6 +60,7 @@ func Server(opts ...Option) http.Service {
assets.Logger(options.Logger),
assets.Config(options.Config),
),
options.Config.HTTP.CacheTTL,
))
mux.Route(options.Config.HTTP.Root, func(r chi.Router) {