Enable gzip compression

This commit is contained in:
heyarne 2022-03-08 10:07:11 +01:00
commit 226e43e2f1
5 changed files with 12 additions and 3 deletions

1
.gitignore vendored
View file

@ -1,6 +1,7 @@
/result
/inkpot-cli
*.epd
*.gz
*.jpg
*.jpeg
*.png

View file

@ -9,6 +9,7 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gin-contrib/gzip"
"github.com/saunaclub/inkpot-cli/epd"
"github.com/spf13/cobra"
)
@ -29,6 +30,7 @@ var serveCmd = &cobra.Command{
Short: "Run a webserver to convert images via HTTP",
Run: func(cmd *cobra.Command, args []string) {
router := gin.Default()
router.Use(gzip.Gzip(gzip.DefaultCompression))
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB
router.GET("/", getIndex)
@ -49,16 +51,18 @@ A webserver to convert GIFs, PNGs and JPEGs to 4-bit grayscale images.
- GET /convert/[url] can be used to convert a file publically accessible via URL
Both routes accept a "width" and a "height" parameter to configure the output size.
If you want the response to be gzip encoded, just set the Accept-Encoding header.
## Examples
Via curl:
Via curl, with GZIP compression:
curl -X POST http://localhost:8080/convert \
-F "file=@my_cat.jpeg" \
-H "Content-Type: multipart/form-data"
-H "Accept-Encoding: gzip"
Via httpie:
Via httpie (which will request a GZIP compressed image by default):
http --form POST :8080/convert file@my_cat.jpg
`

View file

@ -5,7 +5,7 @@ buildGoModule rec {
version = "0.0.1";
src = ./.;
vendorSha256 = "sha256-W+oAjjRYXoKM20nubO0y2yUA4WRjOn7zki3pIf9TMvc=";
vendorSha256 = "sha256-+NV7tsw76n4PIYjeSU9SqyzMdv7OoRJA1TANqhJcR9I=";
buildInputs = [
stdenv

1
go.mod
View file

@ -3,6 +3,7 @@ module github.com/saunaclub/inkpot-cli
go 1.16
require (
github.com/gin-contrib/gzip v0.0.5
github.com/gin-gonic/gin v1.7.7
github.com/spf13/cobra v1.3.0
golang.org/x/image v0.0.0-20190802002840-cff245a6509b

3
go.sum
View file

@ -107,8 +107,11 @@ github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/gzip v0.0.5 h1:mhnVU32YnnBh2LPH2iqRqsA/eR7SAqRaD388jL2s/j0=
github.com/gin-contrib/gzip v0.0.5/go.mod h1:OPIK6HR0Um2vNmBUTlayD7qle4yVVRZT0PyhdUigrKk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=
github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=