Skip to content

Commit

Permalink
Merge pull request #1 from danielwhite/response-compression
Browse files Browse the repository at this point in the history
Add support for response compression
  • Loading branch information
ian-kent committed Oct 6, 2015
2 parents 9f2643f + f47ff9f commit 183e2d6
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/gorilla/pat"
"github.com/gorilla/handlers"
"github.com/ian-kent/go-log/log"
"golang.org/x/crypto/bcrypt"
)
Expand Down Expand Up @@ -74,16 +75,12 @@ func AuthFile(file string) {
}
}

// Listen binds to httpBindAddr
func Listen(httpBindAddr string, Asset func(string) ([]byte, error), exitCh chan int, registerCallback func(http.Handler)) {
log.Info("[HTTP] Binding to address: %s", httpBindAddr)

pat := pat.New()
registerCallback(pat)

// BasicAuthHandler is middleware to check HTTP Basic Authentication
// if an authorisation function is defined.
func BasicAuthHandler(h http.Handler) http.Handler {
f := func(w http.ResponseWriter, req *http.Request) {
if Authorised == nil {
pat.ServeHTTP(w, req)
h.ServeHTTP(w, req)
return
}

Expand All @@ -93,10 +90,23 @@ func Listen(httpBindAddr string, Asset func(string) ([]byte, error), exitCh chan
w.WriteHeader(401)
return
}
pat.ServeHTTP(w, req)
h.ServeHTTP(w, req)
}

err := http.ListenAndServe(httpBindAddr, http.HandlerFunc(f))
return http.HandlerFunc(f)
}

// Listen binds to httpBindAddr
func Listen(httpBindAddr string, Asset func(string) ([]byte, error), exitCh chan int, registerCallback func(http.Handler)) {
log.Info("[HTTP] Binding to address: %s", httpBindAddr)

pat := pat.New()
registerCallback(pat)

compress := handlers.CompressHandler(pat)
auth := BasicAuthHandler(compress)

err := http.ListenAndServe(httpBindAddr, auth)
if err != nil {
log.Fatalf("[HTTP] Error binding to address %s: %s", httpBindAddr, err)
}
Expand Down

0 comments on commit 183e2d6

Please sign in to comment.