Skip to content

Commit

Permalink
Remove Deprecated Fast Router (#191)
Browse files Browse the repository at this point in the history
* Remove Deprecated Fast Router

* rm test

* remote http router from server/kit
  • Loading branch information
marwan-at-work authored and jprobinson committed Feb 11, 2019
1 parent 9eaa369 commit dc1269d
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 125 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require (
github.com/gorilla/mux v1.7.0
github.com/gorilla/websocket v1.4.0
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0
github.com/julienschmidt/httprouter v1.2.0
github.com/kelseyhightower/envconfig v1.3.0
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kelseyhightower/envconfig v1.3.0 h1:IvRS4f2VcIQy6j4ORGIf9145T/AsUB+oY8LyvN8BXNM=
github.com/kelseyhightower/envconfig v1.3.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down
49 changes: 0 additions & 49 deletions server/kit/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"

"github.com/gorilla/mux"
"github.com/julienschmidt/httprouter"
)

// Router is an interface to wrap different router implementations.
Expand All @@ -25,8 +24,6 @@ type RouterOption func(Router) Router
func RouterSelect(name string) RouterOption {
return func(Router) Router {
switch name {
case "fast", "httprouter":
return &fastRouter{httprouter.New()}
case "gorilla":
return &gorillaRouter{mux.NewRouter()}
case "stdlib":
Expand Down Expand Up @@ -115,52 +112,6 @@ func (g *gorillaRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
g.mux.ServeHTTP(w, r)
}

// FastRouter is a Router implementation for `julienschmidt/httprouter`.
type fastRouter struct {
mux *httprouter.Router
}

// Handle will call the `httprouter.METHOD` methods and use the HTTPToFastRoute
// to pass httprouter.Params into a Gorilla request context. The params will be available
// via the `FastRouterVars` function.
func (f *fastRouter) Handle(method, path string, h http.Handler) {
f.mux.Handle(method, path, httpToFastRoute(h))
}

// HandleFunc will call the `httprouter.METHOD` methods and use the HTTPToFastRoute
// to pass httprouter.Params into a Gorilla request context. The params will be available
// via the `FastRouterVars` function.
func (f *fastRouter) HandleFunc(method, path string, h func(http.ResponseWriter, *http.Request)) {
f.Handle(method, path, http.HandlerFunc(h))
}

// SetNotFoundHandler will set httprouter.Router.NotFound.
func (f *fastRouter) SetNotFoundHandler(h http.Handler) {
f.mux.NotFound = h
}

// ServeHTTP will call httprouter.ServerHTTP directly.
func (f *fastRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.mux.ServeHTTP(w, r)
}

// httpToFastRoute will convert an http.Handler to a httprouter.Handle
// by stuffing any route parameters into a Gorilla request context.
// To access the request parameters within the endpoint,
// use the `Vars` function.
func httpToFastRoute(fh http.Handler) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
if len(params) > 0 {
vars := map[string]string{}
for _, param := range params {
vars[param.Key] = param.Value
}
r = SetRouteVars(r, vars)
}
fh.ServeHTTP(w, r)
}
}

// Vars is a helper function for accessing route
// parameters from any server.Router implementation. This is the equivalent
// of using `mux.Vars(r)` with the Gorilla mux.Router.
Expand Down
51 changes: 0 additions & 51 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

"github.com/gorilla/mux"
"github.com/julienschmidt/httprouter"
)

// Router is an interface to wrap different router types to be embedded within
Expand All @@ -21,8 +20,6 @@ type Router interface {
// will default to using Gorilla mux.
func NewRouter(cfg *Config) Router {
switch cfg.RouterType {
case "fast", "httprouter":
return &FastRouter{httprouter.New()}
case "gorilla":
return &GorillaRouter{mux.NewRouter()}
default:
Expand Down Expand Up @@ -60,51 +57,3 @@ func (g *GorillaRouter) SetNotFoundHandler(h http.Handler) {
func (g *GorillaRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
g.mux.ServeHTTP(w, r)
}

// FastRouter is a Router implementation for `julienschmidt/httprouter`. THIS ROUTER IS
// DEPRECATED. Please use gorilla or stdlib if you can. Metrics will not work properly on
// servers using this router type. (see issue #132)
type FastRouter struct {
mux *httprouter.Router
}

// Handle will call the `httprouter.METHOD` methods and use the HTTPToFastRoute
// to pass httprouter.Params into a Gorilla request context. The params will be available
// via the `FastRouterVars` function.
func (f *FastRouter) Handle(method, path string, h http.Handler) {
f.mux.Handle(method, path, HTTPToFastRoute(h))
}

// HandleFunc will call the `httprouter.METHOD` methods and use the HTTPToFastRoute
// to pass httprouter.Params into a Gorilla request context. The params will be available
// via the `FastRouterVars` function.
func (f *FastRouter) HandleFunc(method, path string, h func(http.ResponseWriter, *http.Request)) {
f.Handle(method, path, http.HandlerFunc(h))
}

// SetNotFoundHandler will set httprouter.Router.NotFound.
func (f *FastRouter) SetNotFoundHandler(h http.Handler) {
f.mux.NotFound = h
}

// ServeHTTP will call httprouter.ServerHTTP directly.
func (f *FastRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.mux.ServeHTTP(w, r)
}

// HTTPToFastRoute will convert an http.Handler to a httprouter.Handle
// by stuffing any route parameters into a Gorilla request context.
// To access the request parameters within the endpoint,
// use the `Vars` function.
func HTTPToFastRoute(fh http.Handler) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
if len(params) > 0 {
vars := map[string]string{}
for _, param := range params {
vars[param.Key] = param.Value
}
SetRouteVars(r, vars)
}
fh.ServeHTTP(w, r)
}
}
22 changes: 0 additions & 22 deletions server/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,6 @@ import (
"testing"
)

func TestFastRoute(t *testing.T) {
cfg := &Config{RouterType: "fast", HealthCheckType: "simple", HealthCheckPath: "/status"}
srvr := NewSimpleServer(cfg)
RegisterHealthHandler(cfg, srvr.monitor, srvr.mux)
srvr.Register(&benchmarkSimpleService{true})

w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/svc/v1/1/{something}/blah", nil)
r.RemoteAddr = "0.0.0.0:8080"

srvr.ServeHTTP(w, r)

if w.Code != http.StatusOK {
t.Errorf("SimpleHealthCheck expected 200 response code, got %d", w.Code)
}

wantBody := "blah"
if gotBody := w.Body.String(); gotBody != wantBody {
t.Errorf("Fast route expected response body to be %q, got %q", wantBody, gotBody)
}
}

func TestGorillaRoute(t *testing.T) {
cfg := &Config{HealthCheckType: "simple", HealthCheckPath: "/status"}
srvr := NewSimpleServer(cfg)
Expand Down

0 comments on commit dc1269d

Please sign in to comment.