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

support gzipped GUI files #31056

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
55 changes: 47 additions & 8 deletions comp/core/gui/guiimpl/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package guiimpl

import (
"compress/gzip"
"context"
"crypto/rand"
"embed"
Expand All @@ -19,6 +20,7 @@ import (
"path"
"path/filepath"
"strconv"
"strings"
"time"

"go.uber.org/fx"
Expand Down Expand Up @@ -95,6 +97,10 @@ type provides struct {
// @param deps dependencies needed to construct the gui, bundled in a struct
// @return an optional, depending of "GUI_port" configuration value
func newGui(deps dependencies) provides {
_ = mime.AddExtensionType(".eot", "application/vnd.ms-fontobject")
_ = mime.AddExtensionType(".ttf", "application/font-sfnt")
_ = mime.AddExtensionType(".woff", "font/woff")
_ = mime.AddExtensionType(".woff2", "font/woff2")

p := provides{
Comp: optional.NewNoneOption[guicomp.Component](),
Expand Down Expand Up @@ -228,9 +234,45 @@ func renderIndexPage(w http.ResponseWriter, _ *http.Request) {
}
}

func serveAsset(w http.ResponseWriter, req *http.Request, assetPath string) ([]byte, error) {
var rdr io.ReadCloser
isGzipped := true
rdr, err := viewsFS.Open(assetPath + ".gz")
if err != nil && os.IsNotExist(err) {
isGzipped = false
rdr, err = viewsFS.Open(assetPath)
}
brycekahle marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
defer rdr.Close()

if isGzipped {
// if client supports gzip, return as-is
if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
} else {
// ungzip on the fly
gzReader, err := gzip.NewReader(rdr)
if err != nil {
return nil, err
}
// ensure underlying io.Reader is closed
defer gzReader.Close()
brycekahle marked this conversation as resolved.
Show resolved Hide resolved
rdr = gzReader
}
}

data, err := io.ReadAll(rdr)
if err != nil {
return nil, err
}
return data, nil
}

func serveAssets(w http.ResponseWriter, req *http.Request) {
path := path.Join("views", "private", req.URL.Path)
data, err := viewsFS.ReadFile(path)
assetPath := path.Join("views", "private", req.URL.Path)
data, err := serveAsset(w, req, assetPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we group all header logic together?

Here is a suggestion of how it could look like:

func serveAsset(gzipSupport bool, assetPath string) ([]byte, error) {
	var rdr io.ReadCloser
	rdr, err := viewsFS.Open(assetPath + ".gz")
	if err != nil && os.IsNotExist(err) {
		rdr, err = viewsFS.Open(assetPath)
	}
	if err != nil {
		return nil, err
	}
	defer rdr.Close()

	if !gzipSupport {
		// ungzip on the fly
		gzReader, err := gzip.NewReader(rdr)
		if err != nil {
			return nil, err
		}
		// ensure underlying io.Reader is closed
		defer gzReader.Close()
		rdr = gzReader
	}

	data, err := io.ReadAll(rdr)
	if err != nil {
		return nil, err
	}
	return data, nil
}

func serveAssets(w http.ResponseWriter, req *http.Request) {
	assetPath := path.Join("views", "private", req.URL.Path)
	gzipSupport := strings.Contains(req.Header.Get("Accept-Encoding"), "gzip")
	data, err := serveAsset(gzipSupport, assetPath)
	if err != nil {
		if os.IsNotExist(err) {
			http.Error(w, err.Error(), http.StatusNotFound)
		} else {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
		return
	}
	
	if gzipSupport {
		w.Header().Set("Content-Encoding", "gzip")
	}
	
	w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(assetPath)))
	w.Header().Set("Content-Length", strconv.Itoa(len(data)))
	_, _ = w.Write(data)
}

Let me know what you think :smi

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

if err != nil {
if os.IsNotExist(err) {
http.Error(w, err.Error(), http.StatusNotFound)
Expand All @@ -239,13 +281,10 @@ func serveAssets(w http.ResponseWriter, req *http.Request) {
}
return
}
ctype := mime.TypeByExtension(filepath.Ext(path))
if ctype == "" {
ctype = http.DetectContentType(data)
}
w.Header().Set("Content-Type", ctype)

w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(assetPath)))
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.Write(data)
_, _ = w.Write(data)
}

func (g *gui) getAccessToken(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading
Loading