Skip to content

Commit

Permalink
feat: Add gz compression for html response
Browse files Browse the repository at this point in the history
Pre-compress the HTML page to avoid slow page loads when more and/or high res images are added
  • Loading branch information
timo-reymann committed Mar 29, 2024
1 parent f18bdb2 commit 05bd2fe
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
19 changes: 14 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
_ "embed"
"flag"
"github.com/timo-reymann/yal/pkg/buildinfo"
"github.com/timo-reymann/yal/pkg/compress"
"github.com/timo-reymann/yal/pkg/config"
"github.com/timo-reymann/yal/pkg/templating"
"log"
"net/http"
_ "net/http/pprof"
"os"
)

func renderTemplate(templateFile string) (string, error) {
func renderTemplate(templateFile string) ([]byte, error) {
c, err := config.Load()
if err != nil {
return "", err
return nil, err
}

return templating.RenderTemplate(templateFile, c)
Expand Down Expand Up @@ -46,7 +48,7 @@ func Run() {
log.Println("Rendering " + *renderOutput)

if *renderOutput == "-" {
_, _ = os.Stdout.WriteString(rendered)
_, _ = os.Stdout.Write(rendered)
return
}

Expand All @@ -56,16 +58,23 @@ func Run() {
}
defer templatedFile.Close()

_, err = templatedFile.WriteString(rendered)
_, err = templatedFile.Write(rendered)
if err != nil {
log.Fatalf("Failed to write templated content to file: %s", err.Error())
}
}

if *serve {
renderedGz, err := compress.GzBytes(rendered)
if err != nil {
log.Fatalf("Failed to compress HTML page for HTTP server: %s", err.Error())
}

log.Println("Starting server on 0.0.0.0:" + config.Port())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(rendered))
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Content-Encoding", "gzip")
_, _ = w.Write(renderedGz)
})

log.Fatal(http.ListenAndServe(":"+config.Port(), nil))
Expand Down
19 changes: 19 additions & 0 deletions pkg/compress/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package compress

import (
"bytes"
"compress/gzip"
)

// GzBytes takes a given byte array and returns a compressed version using gz
func GzBytes(data []byte) ([]byte, error) {
var renderedGzBuffer bytes.Buffer
gzw := gzip.NewWriter(&renderedGzBuffer)
if _, err := gzw.Write(data); err != nil {
return nil, err
}
gzd := renderedGzBuffer.Bytes()
renderedGzBuffer.Reset()

return gzd, nil
}
32 changes: 32 additions & 0 deletions pkg/compress/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package compress

import (
"bytes"
"compress/gzip"
"testing"
)

func TestGzBytes(t *testing.T) {
// Test data
inputData := []byte("hello, world")

// Call the function
_, err := GzBytes(inputData)
if err != nil {
t.Fatalf("Expected gz compress to work for buffer, but failed: %v", err)
}
}

// Helper function to compress data using gzip
func compressData(data []byte) ([]byte, error) {
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
_, err := gz.Write(data)
if err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
10 changes: 5 additions & 5 deletions pkg/templating/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
//go:embed index.gohtml
var DefaultTemplate string

func RenderTemplate(path string, c *config.Configuration) (string, error) {
func RenderTemplate(path string, c *config.Configuration) ([]byte, error) {
var htmlFile []byte
var err error
if path == "builtin" {
htmlFile = []byte(DefaultTemplate)
} else {
htmlFile, err = os.ReadFile(path)
if err != nil {
return "", err
return nil, err
}
}

Expand All @@ -32,14 +32,14 @@ func RenderTemplate(path string, c *config.Configuration) (string, error) {
})
tmpl, err = tmpl.Parse(string(htmlFile))
if err != nil {
return "", err
return nil, err
}

var outputBuffer bytes.Buffer
err = tmpl.Execute(&outputBuffer, c)
if err != nil {
return "", err
return nil, err
}

return outputBuffer.String(), nil
return outputBuffer.Bytes(), nil
}
4 changes: 2 additions & 2 deletions pkg/templating/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestRenderTemplate_Default(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if templated == "" {
if templated == nil {
t.Fatal("Expected template to be rendered for default")
}
}
Expand All @@ -32,7 +32,7 @@ func TestRenderTemplate_DefaultFile(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if templated == "" {
if templated == nil {
t.Fatal("Expected template to be rendered for default")
}
}

0 comments on commit 05bd2fe

Please sign in to comment.