-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add gz compression for html response
Pre-compress the HTML page to avoid slow page loads when more and/or high res images are added
- Loading branch information
1 parent
f18bdb2
commit 05bd2fe
Showing
5 changed files
with
72 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters