Skip to content

Commit

Permalink
[feature] Gin enable gzip encoding (#405)
Browse files Browse the repository at this point in the history
* add gin gzip dependency

* add gzip middleware to router

* go mod tidy
  • Loading branch information
tsmethurst authored Feb 19, 2022
1 parent 23034ec commit a089a98
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ The following libraries and frameworks are used by GoToSocial, with gratitude
- [coreos/go-oidc](https://github.com/coreos/go-oidc); OIDC client library. [Apache-2.0 License](https://spdx.org/licenses/Apache-2.0.html).
- [gin-gonic/gin](https://github.com/gin-gonic/gin); speedy router engine. [MIT License](https://spdx.org/licenses/MIT.html).
- [gin-contrib/cors](https://github.com/gin-contrib/cors); Gin CORS middleware. [MIT License](https://spdx.org/licenses/MIT.html).
- [gin-contrib/gzip](https://github.com/gin-contrib/gzip); Gin gzip middleware. [MIT License](https://spdx.org/licenses/MIT.html).
- [gin-contrib/sessions](https://github.com/gin-contrib/sessions); Gin sessions middleware. [MIT License](https://spdx.org/licenses/MIT.html)
- [gin-contrib/static](https://github.com/gin-contrib/static); Gin static page middleware. [MIT License](https://spdx.org/licenses/MIT.html)
- [go-fed/httpsig](https://github.com/go-fed/httpsig); secure HTTP signature library. [BSD-3-Clause License](https://spdx.org/licenses/BSD-3-Clause.html).
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/buckket/go-blurhash v1.1.0
github.com/coreos/go-oidc/v3 v3.1.0
github.com/gin-contrib/cors v1.3.1
github.com/gin-contrib/gzip v0.0.5
github.com/gin-contrib/sessions v0.0.4
github.com/gin-gonic/gin v1.7.7
github.com/go-fed/httpsig v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ github.com/gavv/httpexpect v2.0.0+incompatible/go.mod h1:x+9tiU1YnrOvnB725RkpoLv
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/cors v1.3.1 h1:doAsuITavI4IOcd0Y19U4B+O0dNWihRyX//nn4sEmgA=
github.com/gin-contrib/cors v1.3.1/go.mod h1:jjEJ4268OPZUcU7k9Pm653S7lXUGcqMADzFA61xsmDk=
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/sessions v0.0.4 h1:gq4fNa1Zmp564iHP5G6EBuktilEos8VKhe2sza1KMgo=
github.com/gin-contrib/sessions v0.0.4/go.mod h1:pQ3sIyviBBGcxgyR8mkeJuXbeV3h3NYmhJADQTq5+Vo=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down
30 changes: 30 additions & 0 deletions internal/router/gzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
GoToSocial
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package router

import (
ginGzip "github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)

func useGzip(engine *gin.Engine) error {
gzipMiddleware := ginGzip.Gzip(ginGzip.DefaultCompression)
engine.Use(gzipMiddleware)
return nil
}
15 changes: 10 additions & 5 deletions internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,8 @@ func New(ctx context.Context, db db.DB) (Router, error) {
return nil, err
}

// set template functions
LoadTemplateFunctions(engine)

// load templates onto the engine
if err := loadTemplates(engine); err != nil {
// enable gzip compression on the engine
if err := useGzip(engine); err != nil {
return nil, err
}

Expand All @@ -150,6 +147,14 @@ func New(ctx context.Context, db db.DB) (Router, error) {
return nil, err
}

// set template functions
LoadTemplateFunctions(engine)

// load templates onto the engine
if err := loadTemplates(engine); err != nil {
return nil, err
}

// create the http server here, passing the gin engine as handler
bindAddress := viper.GetString(keys.BindAddress)
port := viper.GetInt(keys.Port)
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/gin-contrib/gzip/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

135 changes: 135 additions & 0 deletions vendor/github.com/gin-contrib/gzip/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions vendor/github.com/gin-contrib/gzip/gzip.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions vendor/github.com/gin-contrib/gzip/handler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a089a98

Please sign in to comment.