summaryrefslogtreecommitdiff
path: root/service/http/service.go
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2019-05-04 00:09:12 +0300
committerGitHub <[email protected]>2019-05-04 00:09:12 +0300
commit727602f9d6e3b5a92f6fa0d7a497ab1ef358a349 (patch)
treeec41356d3e421284f44d8c82f3b297e5c8ab2c7a /service/http/service.go
parentf4515e276179d77e3f9457ba0d99545081ae50ea (diff)
parent8a2f79a622e9b3b6e20718a257bfdaaf8dbb8747 (diff)
Merge pull request #144 from spiral/feature/updates
WIP: 1.4.0
Diffstat (limited to 'service/http/service.go')
-rw-r--r--service/http/service.go23
1 files changed, 21 insertions, 2 deletions
diff --git a/service/http/service.go b/service/http/service.go
index ad59f887..651284b4 100644
--- a/service/http/service.go
+++ b/service/http/service.go
@@ -25,7 +25,7 @@ const (
// http middleware type.
type middleware func(f http.HandlerFunc) http.HandlerFunc
-// Service manages rr, http servers.
+// Services manages rr, http servers.
type Service struct {
cfg *Config
env env.Environment
@@ -33,11 +33,17 @@ type Service struct {
mdwr []middleware
mu sync.Mutex
rr *roadrunner.Server
+ watcher roadrunner.Watcher
handler *Handler
http *http.Server
https *http.Server
}
+// Watch attaches watcher.
+func (s *Service) Watch(w roadrunner.Watcher) {
+ s.watcher = w
+}
+
// AddMiddleware adds new net/http mdwr.
func (s *Service) AddMiddleware(m middleware) {
s.mdwr = append(s.mdwr, m)
@@ -53,6 +59,7 @@ func (s *Service) AddListener(l func(event int, ctx interface{})) {
func (s *Service) Init(cfg *Config, r *rpc.Service, e env.Environment) (bool, error) {
s.cfg = cfg
s.env = e
+
if r != nil {
if err := r.Register(ID, &rpcServer{s}); err != nil {
return false, err
@@ -77,6 +84,10 @@ func (s *Service) Serve() error {
s.rr = roadrunner.NewServer(s.cfg.Workers)
s.rr.Listen(s.throw)
+ if s.watcher != nil {
+ s.rr.Watch(s.watcher)
+ }
+
s.handler = &Handler{cfg: s.cfg, rr: s.rr}
s.handler.Listen(s.throw)
@@ -102,7 +113,7 @@ func (s *Service) Serve() error {
return <-err
}
-// Stop stops the svc.
+// Detach stops the svc.
func (s *Service) Stop() {
s.mu.Lock()
defer s.mu.Unlock()
@@ -117,6 +128,14 @@ func (s *Service) Stop() {
go s.http.Shutdown(context.Background())
}
+// Server returns associated roadrunner server (if any).
+func (s *Service) Server() *roadrunner.Server {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ return s.rr
+}
+
// ServeHTTP handles connection using set of middleware and rr PSR-7 server.
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if s.https != nil && r.TLS == nil && s.cfg.SSL.Redirect {