Skip to content

Commit

Permalink
Merge pull request #204 from flimzy/chiGzip
Browse files Browse the repository at this point in the history
use chi's compression middleware
  • Loading branch information
flimzy authored Oct 1, 2017
2 parents b4c3dad + fd238c4 commit e83984d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 23 deletions.
6 changes: 2 additions & 4 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ ignore:
- github.com/gopherjs/gopherjs
- github.com/gopherjs/jsbuiltin
import:
- package: github.com/NYTimes/gziphandler
version: d8206bba2a6097c0d75f4ac204a4814e47125cdd
- package: github.com/ajg/form
version: ~1.5.0
- package: github.com/pressly/chi
version: ~2.1.0
- package: github.com/go-chi/chi
version: ~3.1.0
- package: github.com/flimzy/diff
version: ^0.1.2
- package: github.com/justinas/alice
Expand Down
2 changes: 1 addition & 1 deletion serve/couchserver/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package couchserver
import (
"net/http"

"github.com/pressly/chi"
"github.com/go-chi/chi"
)

// DB returns the db name in this request, or "" if none.
Expand Down
9 changes: 5 additions & 4 deletions serve/couchserver/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (
"net/http/httptest"
"testing"

"github.com/pressly/chi"
"github.com/go-chi/chi"
)

func TestDB(t *testing.T) {
router := chi.NewRouter()
var result string
router.Get("/:db", func(_ http.ResponseWriter, r *http.Request) {
router.Get("/{db}", func(_ http.ResponseWriter, r *http.Request) {
result = DB(r)
})
req := httptest.NewRequest("GET", "/foo", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if result != "foo" {
t.Errorf("Expected '%s', Got '%s'", "foo", result)
expected := "foo"
if result != expected {
t.Errorf("Expected '%s', Got '%s'", expected, result)
}
}
10 changes: 5 additions & 5 deletions serve/couchserver/couchserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"net/http"

"github.com/pressly/chi"
"github.com/go-chi/chi"

"github.com/flimzy/kivik"
)
Expand Down Expand Up @@ -90,10 +90,10 @@ func (h *Handler) Main() http.Handler {
r.Get("/", h.GetRoot())
r.Get("/favicon.ico", h.GetFavicon())
r.Get("/_all_dbs", h.GetAllDBs())
r.Get("/:db", h.GetDB())
r.Put("/:db", h.PutDB())
r.Head("/:db", h.HeadDB())
r.Post("/:db/_ensure_full_commit", h.Flush())
r.Get("/{db}", h.GetDB())
r.Put("/{db}", h.PutDB())
r.Head("/{db}", h.HeadDB())
r.Post("/{db}/_ensure_full_commit", h.Flush())
r.Get("/_session", h.GetSession())
return r
}
Expand Down
11 changes: 2 additions & 9 deletions serve/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"os"

"github.com/NYTimes/gziphandler"
"github.com/go-chi/chi/middleware"
"github.com/justinas/alice"

"github.com/flimzy/kivik/serve/couchserver"
Expand Down Expand Up @@ -38,13 +38,6 @@ func gzipHandler(s *Service) func(http.Handler) http.Handler {
if level == 0 {
level = 8
}
gzipHandler, err := gziphandler.NewGzipLevelHandler(int(level))
if err != nil {
fmt.Fprintf(os.Stderr, "invalid httpd.compression_level %d\n", level)
return func(h http.Handler) http.Handler {
return h
}
}
fmt.Fprintf(os.Stderr, "Enabling gzip compression, level %d\n", level)
return gzipHandler
return middleware.Compress(level, "text/plain", "text/html", "application/json")
}

0 comments on commit e83984d

Please sign in to comment.