From 226e43e2f1bc570cc09b7fe7d312ae545ffedf75 Mon Sep 17 00:00:00 2001 From: heyarne Date: Tue, 8 Mar 2022 10:07:11 +0100 Subject: [PATCH] Enable gzip compression --- .gitignore | 1 + cmd/serve.go | 8 ++++++-- default.nix | 2 +- go.mod | 1 + go.sum | 3 +++ 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 5a53253..8d22ec7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ /result /inkpot-cli *.epd +*.gz *.jpg *.jpeg *.png diff --git a/cmd/serve.go b/cmd/serve.go index 33e7685..ca91cbb 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -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 ` diff --git a/default.nix b/default.nix index 5fd3368..d5f03ba 100644 --- a/default.nix +++ b/default.nix @@ -5,7 +5,7 @@ buildGoModule rec { version = "0.0.1"; src = ./.; - vendorSha256 = "sha256-W+oAjjRYXoKM20nubO0y2yUA4WRjOn7zki3pIf9TMvc="; + vendorSha256 = "sha256-+NV7tsw76n4PIYjeSU9SqyzMdv7OoRJA1TANqhJcR9I="; buildInputs = [ stdenv diff --git a/go.mod b/go.mod index 81abb6a..115d256 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 642f4ff..b482938 100644 --- a/go.sum +++ b/go.sum @@ -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=