diff options
Diffstat (limited to 'service')
-rw-r--r-- | service/http/service.go | 12 | ||||
-rw-r--r-- | service/http/service_test.go | 15 | ||||
-rw-r--r-- | service/static/service.go | 11 |
3 files changed, 24 insertions, 14 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() }() diff --git a/service/static/service.go b/service/static/service.go index 2324dcd1..add242e4 100644 --- a/service/static/service.go +++ b/service/static/service.go @@ -56,7 +56,16 @@ func (s *Service) Serve() error { return nil } func (s *Service) Stop() {} // middleware must return true if request/response pair is handled within the middleware. -func (s *Service) middleware(w http.ResponseWriter, r *http.Request) bool { +func (s *Service) middleware(f http.HandlerFunc) http.HandlerFunc { + // Define the http.HandlerFunc + return func(w http.ResponseWriter, r *http.Request) { + if !s.handleStatic(w, r) { + f(w, r) + } + } +} + +func (s *Service) handleStatic(w http.ResponseWriter, r *http.Request) bool { fPath := r.URL.Path if !strings.HasPrefix(fPath, "/") { |