diff options
Diffstat (limited to 'service/http/service.go')
-rw-r--r-- | service/http/service.go | 58 |
1 files changed, 51 insertions, 7 deletions
diff --git a/service/http/service.go b/service/http/service.go index 8105d218..b309cd45 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -7,8 +7,10 @@ import ( "github.com/spiral/roadrunner/service/env" "github.com/spiral/roadrunner/service/http/attributes" "github.com/spiral/roadrunner/service/rpc" + "github.com/spiral/roadrunner/util" "golang.org/x/net/http2" "net/http" + "net/http/fcgi" "net/url" "strings" "sync" @@ -37,6 +39,7 @@ type Service struct { handler *Handler http *http.Server https *http.Server + fcgi *http.Server } // Attach attaches controller. Currently only one controller is supported. @@ -66,6 +69,10 @@ func (s *Service) Init(cfg *Config, r *rpc.Service, e env.Environment) (bool, er } } + if !cfg.EnableHTTP() && !cfg.EnableTLS() && !cfg.EnableFCGI() { + return false, nil + } + return true, nil } @@ -91,12 +98,18 @@ func (s *Service) Serve() error { s.handler = &Handler{cfg: s.cfg, rr: s.rr} s.handler.Listen(s.throw) - s.http = &http.Server{Addr: s.cfg.Address, Handler: s} + if s.cfg.EnableHTTP() { + s.http = &http.Server{Addr: s.cfg.Address, Handler: s} + } if s.cfg.EnableTLS() { s.https = s.initSSL() } + if s.cfg.EnableFCGI() { + s.fcgi = &http.Server{Handler: s} + } + s.mu.Unlock() if err := s.rr.Start(); err != nil { @@ -104,10 +117,24 @@ func (s *Service) Serve() error { } defer s.rr.Stop() - err := make(chan error, 2) - go func() { err <- s.http.ListenAndServe() }() + err := make(chan error, 3) + + if s.http != nil { + go func() { + err <- s.http.ListenAndServe() + }() + } + if s.https != nil { - go func() { err <- s.https.ListenAndServeTLS(s.cfg.SSL.Cert, s.cfg.SSL.Key) }() + go func() { + err <- s.https.ListenAndServeTLS(s.cfg.SSL.Cert, s.cfg.SSL.Key) + }() + } + + if s.fcgi != nil { + go func() { + err <- s.ListenAndServeFCGI() + }() } return <-err @@ -117,15 +144,18 @@ func (s *Service) Serve() error { func (s *Service) Stop() { s.mu.Lock() defer s.mu.Unlock() - if s.http == nil { - return + + if s.fcgi != nil { + go s.fcgi.Shutdown(context.Background()) } if s.https != nil { go s.https.Shutdown(context.Background()) } - go s.http.Shutdown(context.Background()) + if s.http != nil { + go s.http.Shutdown(context.Background()) + } } // Server returns associated rr server (if any). @@ -136,6 +166,20 @@ func (s *Service) Server() *roadrunner.Server { return s.rr } +func (s *Service) ListenAndServeFCGI() error { + l, err := util.CreateListener(s.cfg.FCGI.Address); + if err != nil { + return err + } + + err = fcgi.Serve(l, s.fcgi.Handler) + if err != nil { + return err + } + + return nil +} + // 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 { |