Skip to content

Commit

Permalink
closes #47 add support for gzip compression for all http responses (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbolo committed Jan 16, 2019
1 parent 53d7bf5 commit acfa10e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func printConfigSummary() {
"server.bind_port",
"server.tls.enabled",
"server.access_log",
"server.compression",
"aws.polling_interval",
"aws.required_tag_key",
"aws.required_tag_value",
Expand Down
13 changes: 12 additions & 1 deletion backend/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"

"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -83,8 +84,12 @@ func newRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {

// add compression support to handler if enabled
var handler http.Handler
handler = route.HandlerFunc
if viper.GetBool("server.compression") {
handler = handlers.CompressHandler(route.HandlerFunc)
}

// add routes to mux
router.
Expand All @@ -100,10 +105,16 @@ func newRouter() *mux.Router {
staticPath = "./frondent/dist"
}

handlerStatic := http.StripPrefix("/", http.FileServer(http.Dir(staticPath)))
// add compression support to handler if enabled
if viper.GetBool("server.compression") {
handlerStatic = handlers.CompressHandler(handlerStatic)
}

router.
Methods("GET").
PathPrefix("/").
Handler(http.StripPrefix("/", http.FileServer(http.Dir(staticPath))))
Handler(handlerStatic)

return router
}
4 changes: 4 additions & 0 deletions testdata/sampleconfig/power-toggle-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ server:
# enable access log on stdout
access_log: false

# enable supported compression of http responses when client requests for it
# currently only gzip is supported
compression: true

# TLS options
tls:
# enables TLS
Expand Down

0 comments on commit acfa10e

Please sign in to comment.