change caching for console (service worker) (#261)

This commit is contained in:
Livio Amstutz
2020-06-24 14:26:27 +02:00
committed by GitHub
parent ddebaad497
commit b88f200434
3 changed files with 33 additions and 11 deletions

View File

@@ -54,6 +54,8 @@ export ZITADEL_CSRF_DEV=true
#CACHE #CACHE
export ZITADEL_CACHE_MAXAGE=12h export ZITADEL_CACHE_MAXAGE=12h
export ZITADEL_CACHE_SHARED_MAXAGE=168h export ZITADEL_CACHE_SHARED_MAXAGE=168h
export ZITADEL_SHORT_CACHE_MAXAGE=5m
export ZITADEL_SHORT_CACHE_SHARED_MAXAGE=15min
#Console #Console
export ZITADEL_CONSOLE_ENV_DIR=../../console/src/assets/ export ZITADEL_CONSOLE_ENV_DIR=../../console/src/assets/

View File

@@ -205,9 +205,12 @@ Admin:
Console: Console:
Port: 50050 Port: 50050
EnvOverwriteDir: $ZITADEL_CONSOLE_ENV_DIR EnvOverwriteDir: $ZITADEL_CONSOLE_ENV_DIR
Cache: ShortCache:
MaxAge: $ZITADEL_CACHE_MAXAGE MaxAge: $ZITADEL_SHORT_CACHE_MAXAGE
SharedMaxAge: $ZITADEL_CACHE_SHARED_MAXAGE SharedMaxAge: $ZITADEL_SHORT_CACHE_SHARED_MAXAGE
LongCache:
MaxAge: $ZITADEL_CACHE_MAXAGE
SharedMaxAge: $ZITADEL_CACHE_SHARED_MAXAGE
CSPDomain: $ZITADEL_DEFAULT_DOMAIN CSPDomain: $ZITADEL_DEFAULT_DOMAIN

View File

@@ -17,7 +17,8 @@ import (
type Config struct { type Config struct {
Port string Port string
EnvOverwriteDir string EnvOverwriteDir string
Cache middleware.CacheConfig ShortCache middleware.CacheConfig
LongCache middleware.CacheConfig
CSPDomain string CSPDomain string
} }
@@ -28,8 +29,17 @@ type spaHandler struct {
const ( const (
envRequestPath = "/assets/environment.json" envRequestPath = "/assets/environment.json"
envDefaultDir = "/console/" envDefaultDir = "/console/"
)
manifestFile = "/manifest.webmanifest" var (
paths = []string{
"/index.html",
"/manifest.webmanifest",
"/ngsw.json",
"/ngsw-worker.js",
"/safety-worker.js",
"/worker-basic.min.js",
}
) )
func (i *spaHandler) Open(name string) (http.File, error) { func (i *spaHandler) Open(name string) (http.File, error) {
@@ -50,7 +60,12 @@ func Start(ctx context.Context, config Config) error {
if config.EnvOverwriteDir != "" { if config.EnvOverwriteDir != "" {
envDir = config.EnvOverwriteDir envDir = config.EnvOverwriteDir
} }
cache := AssetsCacheInterceptorIgnoreManifest(config.Cache.MaxAge.Duration, config.Cache.SharedMaxAge.Duration) cache := AssetsCacheInterceptorIgnoreManifest(
config.ShortCache.MaxAge.Duration,
config.ShortCache.SharedMaxAge.Duration,
config.LongCache.MaxAge.Duration,
config.LongCache.SharedMaxAge.Duration,
)
security := middleware.SecurityHeaders(csp(config.CSPDomain), nil) security := middleware.SecurityHeaders(csp(config.CSPDomain), nil)
http.Handle("/", cache(security(http.FileServer(&spaHandler{statikFS})))) http.Handle("/", cache(security(http.FileServer(&spaHandler{statikFS}))))
http.Handle(envRequestPath, cache(security(http.StripPrefix("/assets", http.FileServer(http.Dir(envDir)))))) http.Handle(envRequestPath, cache(security(http.StripPrefix("/assets", http.FileServer(http.Dir(envDir))))))
@@ -72,14 +87,16 @@ func csp(zitadelDomain string) *middleware.CSP {
return &csp return &csp
} }
func AssetsCacheInterceptorIgnoreManifest(maxAge, sharedMaxAge time.Duration) func(http.Handler) http.Handler { func AssetsCacheInterceptorIgnoreManifest(shortMaxAge, shortSharedMaxAge, longMaxAge, longSharedMaxAge time.Duration) func(http.Handler) http.Handler {
return func(handler http.Handler) http.Handler { return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == manifestFile { for _, path := range paths {
middleware.NoCacheInterceptor(handler).ServeHTTP(w, r) if r.URL.Path == path {
return middleware.AssetsCacheInterceptor(shortMaxAge, shortSharedMaxAge, handler).ServeHTTP(w, r)
return
}
middleware.AssetsCacheInterceptor(longMaxAge, longSharedMaxAge, handler).ServeHTTP(w, r)
} }
middleware.AssetsCacheInterceptor(maxAge, sharedMaxAge, handler).ServeHTTP(w, r)
}) })
} }
} }