diff options
author | Dmitry Patsura <[email protected]> | 2019-06-13 17:46:22 +0300 |
---|---|---|
committer | Dmitry Patsura <[email protected]> | 2019-06-13 20:01:23 +0300 |
commit | f427a13ac091ce5ef814c58870782105adf48c12 (patch) | |
tree | 21de0a765af298b3f5c03fab7a5c8256e7649650 | |
parent | 82cfc4f8f89252083aa09e8370b9605d38808b70 (diff) |
Feature: FastCGI - initial #115
-rw-r--r-- | service/http/config.go | 11 | ||||
-rw-r--r-- | service/http/service.go | 43 |
2 files changed, 51 insertions, 3 deletions
diff --git a/service/http/config.go b/service/http/config.go index ecd5a3a8..585216d5 100644 --- a/service/http/config.go +++ b/service/http/config.go @@ -18,6 +18,8 @@ type Config struct { // SSL defines https server options. SSL SSLConfig + FCGI FCGIConfig + // MaxRequestSize specified max size for payload body in megabytes, set 0 to unlimited. MaxRequestSize int64 @@ -32,6 +34,11 @@ type Config struct { Workers *roadrunner.ServerConfig } +type FCGIConfig struct { + // Port and port to handle as http server. + Address string +} + // SSLConfig defines https server configuration. type SSLConfig struct { // Port to listen as HTTPS server, defaults to 443. @@ -52,6 +59,10 @@ func (c *Config) EnableTLS() bool { return c.SSL.Key != "" || c.SSL.Cert != "" } +func (c *Config) EnableFCGI() bool { + return c.FCGI.Address != "" +} + // Hydrate must populate Config values using given Config source. Must return error if Config is not valid. func (c *Config) Hydrate(cfg service.Config) error { if c.Workers == nil { diff --git a/service/http/service.go b/service/http/service.go index 8105d218..8db13c15 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. @@ -97,6 +100,10 @@ func (s *Service) Serve() error { s.https = s.initSSL() } + if s.cfg.EnableFCGI() { + s.fcgi = &http.Server{Addr: s.cfg.Address, Handler: s} + } + s.mu.Unlock() if err := s.rr.Start(); err != nil { @@ -104,10 +111,22 @@ 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) + + 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 @@ -121,6 +140,10 @@ func (s *Service) Stop() { return } + if s.fcgi != nil { + go s.fcgi.Shutdown(context.Background()) + } + if s.https != nil { go s.https.Shutdown(context.Background()) } @@ -136,6 +159,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.http.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 { |