private/web: make caching headers reusable

Move storagnode/console caching headers to private/web. Also,
start using them in multinode/console/server.

Change-Id: I1f0f3c9833a183476009737cece515ae7537fb83
This commit is contained in:
Egon Elbre 2022-03-11 11:19:11 +02:00
parent 28c9403702
commit 5f7ea1358d
4 changed files with 68 additions and 58 deletions

View File

@ -24,6 +24,7 @@ import (
"storj.io/storj/multinode/payouts"
"storj.io/storj/multinode/reputation"
"storj.io/storj/multinode/storage"
"storj.io/storj/private/web"
)
var (
@ -132,7 +133,8 @@ func NewServer(log *zap.Logger, listener net.Listener, assets fs.FS, services Se
reputationRouter := apiRouter.PathPrefix("/reputation").Subrouter()
reputationRouter.HandleFunc("/satellites/{satelliteID}", reputationController.Stats)
router.PathPrefix("/static").Handler(http.FileServer(http.FS(server.assets)))
staticServer := http.FileServer(http.FS(server.assets))
router.PathPrefix("/static").Handler(web.CacheHandler(staticServer))
router.PathPrefix("/").HandlerFunc(server.appHandler)
server.http = http.Server{

63
private/web/cache.go Normal file
View File

@ -0,0 +1,63 @@
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package web
import (
"net/http"
"path"
"strings"
)
// CacheHandler is a middleware for caching static files for 1 year.
func CacheHandler(fn http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
header := w.Header()
// "mime" package, which http.FileServer uses, depends on Operating System
// configuration for mime-types. When a system has hardcoded mime-types to
// something else, they might serve ".js" as a "plain/text".
//
// Override any of that default behavior to ensure we get the correct types for
// common files.
if contentType, ok := CommonContentType(path.Ext(r.URL.Path)); ok {
header.Set("Content-Type", contentType)
}
header.Set("Cache-Control", "public, max-age=31536000")
header.Set("X-Content-Type-Options", "nosniff")
header.Set("Referrer-Policy", "same-origin")
fn.ServeHTTP(w, r)
})
}
// CommonContentType returns content-type for common extensions,
// ignoring OS settings.
func CommonContentType(ext string) (string, bool) {
ext = strings.ToLower(ext)
mime, ok := commonContentType[ext]
return mime, ok
}
var commonContentType = map[string]string{
".css": "text/css; charset=utf-8",
".gif": "image/gif",
".htm": "text/html; charset=utf-8",
".html": "text/html; charset=utf-8",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".js": "application/javascript",
".mjs": "application/javascript",
".otf": "font/otf",
".pdf": "application/pdf",
".png": "image/png",
".svg": "image/svg+xml",
".ttf": "font/ttf",
".wasm": "application/wasm",
".webp": "image/webp",
".xml": "text/xml; charset=utf-8",
".sfnt": "font/sfnt",
".woff": "font/woff",
".woff2": "font/woff2",
}

View File

@ -1,35 +0,0 @@
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package consoleserver
import "strings"
// CommonContentType returns content-type for common extensions.
func CommonContentType(ext string) (string, bool) {
ext = strings.ToLower(ext)
mime, ok := commonContentType[ext]
return mime, ok
}
var commonContentType = map[string]string{
".css": "text/css; charset=utf-8",
".gif": "image/gif",
".htm": "text/html; charset=utf-8",
".html": "text/html; charset=utf-8",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".js": "application/javascript",
".mjs": "application/javascript",
".otf": "font/otf",
".pdf": "application/pdf",
".png": "image/png",
".svg": "image/svg+xml",
".ttf": "font/ttf",
".wasm": "application/wasm",
".webp": "image/webp",
".xml": "text/xml; charset=utf-8",
".sfnt": "font/sfnt",
".woff": "font/woff",
".woff2": "font/woff2",
}

View File

@ -8,7 +8,6 @@ import (
"errors"
"net"
"net/http"
"path/filepath"
"github.com/gorilla/mux"
"github.com/spacemonkeygo/monkit/v3"
@ -17,6 +16,7 @@ import (
"golang.org/x/sync/errgroup"
"storj.io/common/errs2"
"storj.io/storj/private/web"
"storj.io/storj/storagenode/console"
"storj.io/storj/storagenode/console/consoleapi"
"storj.io/storj/storagenode/notifications"
@ -88,7 +88,7 @@ func NewServer(logger *zap.Logger, assets http.FileSystem, notifications *notifi
if assets != nil {
fs := http.FileServer(assets)
router.PathPrefix("/static/").Handler(server.cacheMiddleware(http.StripPrefix("/static", fs)))
router.PathPrefix("/static/").Handler(web.CacheHandler(http.StripPrefix("/static", fs)))
router.PathPrefix("/").Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := r.Clone(r.Context())
req.URL.Path = "/dist/"
@ -129,23 +129,3 @@ func (server *Server) Run(ctx context.Context) (err error) {
func (server *Server) Close() error {
return server.server.Close()
}
// cacheMiddleware is a middleware for caching static files.
func (server *Server) cacheMiddleware(fn http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// "mime" package, which http.FileServer uses, depends on Operating System
// configuration for mime-types. When a system has hardcoded mime-types to
// something else, they might serve ".js" as a "plain/text".
//
// Override any of that default behavior to ensure we get the correct types for
// common files.
if contentType, ok := CommonContentType(filepath.Ext(r.URL.Path)); ok {
w.Header().Set("Content-Type", contentType)
}
w.Header().Set("Cache-Control", "public, max-age=31536000")
w.Header().Set("X-Content-Type-Options", "nosniff")
fn.ServeHTTP(w, r)
})
}