Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Use gorilla/mux router V1 #203

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/http"

"github.com/gorilla/mux"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -105,3 +106,9 @@ func (h ContextHandlerFunc) ServeHTTPContext(ctx context.Context, rw http.Respon
type ContextHandler interface {
ServeHTTPContext(context.Context, http.ResponseWriter, *http.Request)
}

// GorillaService lets you define a gorilla configured
// Router as the main service for SimpleServer
type GorillaService interface {
Gorilla() *mux.Router
}
8 changes: 8 additions & 0 deletions server/simple_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func (s *SimpleServer) Register(svcI Service) error {
ss SimpleService
cs ContextService
mcs MixedContextService
gs GorillaService
)

switch svc := svcI.(type) {
Expand All @@ -216,6 +217,8 @@ func (s *SimpleServer) Register(svcI Service) error {
cs = svc
case ContextService:
cs = svc
case GorillaService:
gs = svc
default:
return errors.New("services for SimpleServers must implement the SimpleService, JSONService or MixedService interfaces")
}
Expand Down Expand Up @@ -259,6 +262,11 @@ func (s *SimpleServer) Register(svcI Service) error {
}
}

if gs != nil {
s.mux = &GorillaRouter{gs.Gorilla()}
s.h = svcI.Middleware(s.mux)
}

RegisterProfiler(s.cfg, s.mux)
return nil
}
Expand Down
34 changes: 34 additions & 0 deletions server/simple_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"net/http/httptest"
"testing"

"github.com/gorilla/mux"
)

type benchmarkContextService struct {
Expand Down Expand Up @@ -494,3 +496,35 @@ func TestNotFoundHandler(t *testing.T) {
t.Errorf("expected response body to be \"\", got %q", gotBody)
}
}

type gorillaService struct {
mux *mux.Router
}

func (gs *gorillaService) Prefix() string {
return ""
}

func (gs *gorillaService) Middleware(h http.Handler) http.Handler {
return h
}

func (gs *gorillaService) Gorilla() *mux.Router {
return gs.mux
}

func TestGorillaService(t *testing.T) {
r := mux.NewRouter()
var called bool
r.HandleFunc("/svc", func(w http.ResponseWriter, r *http.Request) {
called = true
})
ss := NewSimpleServer(nil)
ss.Register(&gorillaService{mux: r})
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/svc", nil)
ss.ServeHTTP(w, req)
if !called {
t.Fatalf("Expected gorilla router to be called: %v", w.Result().Status)
}
}