diff options
author | Wolfy-J <[email protected]> | 2018-06-10 17:04:02 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-10 17:04:02 +0300 |
commit | 3fe85e9d92f5f98337e8f7fd9a14e6b66b9694bd (patch) | |
tree | 2575fbba16df4f95dd392f231ad7d7b30a5dd4ef /service/http/service.go | |
parent | 4c292ee46f5505b00b16186e8f30e9bc1be25895 (diff) |
http service
Diffstat (limited to 'service/http/service.go')
-rw-r--r-- | service/http/service.go | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/service/http/service.go b/service/http/service.go index 5a0d4c16..c31c4a47 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -17,10 +17,11 @@ type Middleware interface { // Service manages rr, http servers. type Service struct { - middleware []Middleware cfg *Config + listener func(event int, ctx interface{}) + middleware []Middleware rr *roadrunner.Server - handler *Handler + srv *Server http *http.Server } @@ -28,6 +29,11 @@ func (s *Service) Add(m Middleware) { s.middleware = append(s.middleware, m) } +// Listen attaches server event watcher. +func (s *Service) Listen(o func(event int, ctx interface{})) { + s.listener = o +} + // Configure must return configure service and return true if service hasStatus enabled. Must return error in case of // misconfiguration. Services must not be used without proper configuration pushed first. func (s *Service) Configure(cfg service.Config, c service.Container) (bool, error) { @@ -56,11 +62,18 @@ func (s *Service) Serve() error { } defer s.rr.Stop() - // todo: observer - s.rr = rr - s.handler = &Handler{cfg: s.cfg, rr: s.rr} - s.http = &http.Server{Addr: s.cfg.httpAddr(), Handler: s} + s.srv = &Server{cfg: s.cfg, rr: s.rr} + s.http = &http.Server{Addr: s.cfg.httpAddr()} + + s.rr.Listen(s.listener) + s.srv.Listen(s.listener) + + if len(s.middleware) == 0 { + s.http.Handler = s.srv + } else { + s.http.Handler = s + } if err := s.http.ListenAndServe(); err != nil { return err @@ -71,6 +84,10 @@ func (s *Service) Serve() error { // Stop stops the service. func (s *Service) Stop() { + if s.http == nil { + return + } + s.http.Shutdown(context.Background()) } @@ -82,5 +99,5 @@ func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } - s.handler.Handle(w, r) + s.srv.ServeHTTP(w, r) } |