Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI Etag support for embedded content #7132

Merged
merged 2 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions pkg/api/etag_middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package api

import (
"crypto/md5" //nolint:gosec
"encoding/hex"
"io"
"io/fs"
"net/http"
"path"
"strings"
)

// EtagMiddleware returns a new Etag middleware handler.
// It designs to work on embedded FS, where the content doesn't change.
// It calculates the Etag for each file on startup and serves it on each request.
func EtagMiddleware(root fs.FS, next http.Handler) http.Handler {
etags, err := scanFSEtags(root)
if err != nil {
panic(err)
}
return &etagHandler{
root: root,
next: next,
etags: etags,
}
}

type etagHandler struct {
root fs.FS
next http.Handler
etags map[string]string
}

func (e *etagHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
upath := r.URL.Path
if !strings.HasPrefix(upath, "/") {
upath = "/" + upath
}
upath = path.Clean(upath)
if strings.HasSuffix(upath, "/") {
upath += "index.html"
}
etag, ok := e.etags[upath]
if ok {
w.Header().Set("Etag", "\""+etag+"\"")
}
e.next.ServeHTTP(w, r)
}

func scanFSEtags(fSys fs.FS) (map[string]string, error) {
etags := make(map[string]string)
err := fs.WalkDir(fSys, ".", func(fpath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}

f, err := fSys.Open(fpath)
if err != nil {
return err
}
defer func() { _ = f.Close() }()

h := md5.New() //nolint:gosec
if _, err := io.Copy(h, f); err != nil {
return err
}
hashValue := hex.EncodeToString(h.Sum(nil))
etags["/"+fpath] = hashValue
return nil
})
if err != nil {
return nil, err
}
return etags, nil
}
7 changes: 3 additions & 4 deletions pkg/api/ui_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

gomime "github.com/cubewise-code/go-mime"
"github.com/go-chi/chi/v5/middleware"
"github.com/treeverse/lakefs/pkg/api/params"
gwerrors "github.com/treeverse/lakefs/pkg/gateway/errors"
"github.com/treeverse/lakefs/pkg/gateway/operations"
Expand All @@ -34,8 +33,8 @@ func NewUIHandler(gatewayDomains []string, snippets []params.CodeSnippet) http.H
panic(err)
}
fileSystem := http.FS(injectedContent)
nocacheContent := middleware.NoCache(http.StripPrefix("/", http.FileServer(fileSystem)))
return NewHandlerWithDefault(fileSystem, nocacheContent, gatewayDomains)
etagHandler := EtagMiddleware(injectedContent, http.StripPrefix("/", http.FileServer(fileSystem)))
return NewHandlerWithDefault(fileSystem, etagHandler, gatewayDomains)
}

func NewS3GatewayEndpointErrorHandler(gatewayDomains []string) http.Handler {
Expand All @@ -45,7 +44,7 @@ func NewS3GatewayEndpointErrorHandler(gatewayDomains []string) http.Handler {
return
}

// For other requests, return generic not found error
// For other requests, return generic "not found" error
w.WriteHeader(http.StatusNotFound)
})
}
Expand Down
Loading