diff options
author | Wolfy-J <[email protected]> | 2018-07-07 19:42:31 -0700 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-07-07 19:42:31 -0700 |
commit | 806b89a1ac24cf6d173f697c9261eed5efa68c3f (patch) | |
tree | e77ff0d9bdf535881ce553574c42d76240ac11a2 /service/http | |
parent | 98f7ce699b559afd74d4e9d607e64b9a8367d762 (diff) |
new middleware format
Diffstat (limited to 'service/http')
-rw-r--r-- | service/http/service.go | 12 | ||||
-rw-r--r-- | service/http/service_test.go | 15 |
2 files changed, 14 insertions, 13 deletions
diff --git a/service/http/service.go b/service/http/service.go index 460175b9..710cd60c 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -13,8 +13,8 @@ import ( // ID contains default svc name. const ID = "http" -// must return true if request/response pair is handled within the middleware. -type middleware func(w http.ResponseWriter, r *http.Request) bool +// http middleware type. +type middleware func(f http.HandlerFunc) http.HandlerFunc // Service manages rr, http servers. type Service struct { @@ -114,13 +114,13 @@ func (s *Service) Stop() { // middleware handles connection using set of mdws and rr PSR-7 server. func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) { r = InitAttributes(r) + + f := s.srv.ServeHTTP for _, m := range s.mdws { - if m(w, r) { - return - } + f = m(f) } - s.srv.ServeHTTP(w, r) + f(w, r) } func (s *Service) listener(event int, ctx interface{}) { diff --git a/service/http/service_test.go b/service/http/service_test.go index 02d1c3f0..50836b4b 100644 --- a/service/http/service_test.go +++ b/service/http/service_test.go @@ -253,14 +253,15 @@ func Test_Service_Middleware(t *testing.T) { assert.NotNil(t, s) assert.Equal(t, service.StatusConfigured, st) - s.(*Service).AddMiddleware(func(w http.ResponseWriter, r *http.Request) bool { - if r.URL.Path == "/halt" { - w.WriteHeader(500) - w.Write([]byte("halted")) - return true + s.(*Service).AddMiddleware(func(f http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/halt" { + w.WriteHeader(500) + w.Write([]byte("halted")) + } else { + f(w, r) + } } - - return false }) go func() { c.Serve() }() |