Skip to content

Commit

Permalink
Add wrapper for using middleware (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzmitry Rudkouski authored and Daniil Kukharau committed Nov 12, 2019
1 parent ee168fa commit 8447e15
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
21 changes: 21 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Server struct {
initializers []InitializerFunc
initializeTimeout time.Duration
registrars []func(mux *http.ServeMux) error
middlewares []Middleware

// GRPCServer will be started whenever this is served
GRPCServer *grpc.Server
Expand All @@ -39,6 +40,9 @@ type Server struct {
HTTPServer *http.Server
}

//Middleware wrapper
type Middleware func(handler http.Handler) http.Handler

// Option is a functional option for creating a Server
type Option func(*Server) error

Expand Down Expand Up @@ -67,6 +71,15 @@ func NewServer(opts ...Option) (*Server, error) {
}
s.HTTPServer.Handler = mux

// Revert user input middlewares
for i, j := 0, len(s.middlewares)-1; i < j; i, j = i+1, j-1 {
s.middlewares[i], s.middlewares[j] = s.middlewares[j], s.middlewares[i]
}

for _, m := range s.middlewares {
s.HTTPServer.Handler = m(s.HTTPServer.Handler)
}

return s, nil
}

Expand Down Expand Up @@ -129,6 +142,14 @@ func WithGateway(options ...gateway.Option) Option {
}
}

// WithMiddlewaries add opportunity to add different middleware
func WithMiddlewares(middleware ...Middleware) Option {
return func(s *Server) error {
s.middlewares = append(s.middlewares, middleware...)
return nil
}
}

// Serve invokes all initializers then serves on the given listeners.
//
// If a listener is left blank, then that particular part will not be served.
Expand Down
44 changes: 44 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,50 @@ func TestWithHandler(t *testing.T) {
}
}

func TestWithMiddlewares(t *testing.T) {
s, err := NewServer(WithMiddlewares(testParamSetter, (&testHttpServer{t}).testParamVerify))
if err != nil {
t.Fatal(err)
}

httpL, err := servertest.NewLocalListener()
if err != nil {
t.Fatal(err)
}

go s.Serve(nil, httpL)
defer s.Stop()

resp, err := http.Get(fmt.Sprint("http://", httpL.Addr().String(), "/v1/hello?name=test"))
if resp.Header.Get("status") != "ok" {
t.Error("Expected another status")
}
if resp.Header.Get("param") != "status" {
t.Error("Expected another status")
}
}
func testParamSetter(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("status", "ok")
h.ServeHTTP(w, r)
})
}

func (t *testHttpServer) testParamVerify(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status := w.Header().Get("status")
if status != "ok" {
t.T.Error("Invalid status")
}
w.Header().Set("param", "status")
h.ServeHTTP(w, r)
})
}

type testHttpServer struct {
*testing.T
}

func TestWithGateway(t *testing.T) {
grpcL, err := servertest.NewLocalListener()
if err != nil {
Expand Down

0 comments on commit 8447e15

Please sign in to comment.