diff options
author | Anton Titov <[email protected]> | 2019-06-14 12:44:45 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2019-06-14 12:44:45 +0300 |
commit | 78724c084ac4db42e713c45ef8cb1453e8352183 (patch) | |
tree | a85abba72b65134a067f4f799b5e1d88a5101d3b /service/http | |
parent | 27e8cb3c4086012968b586fcbd5fd40737be2510 (diff) | |
parent | ac126193fb36fdf248336fa433cbd13602f2ae75 (diff) |
Merge pull request #162 from ovr/fcgi
Feature - FastCGI support for HTTP Service
Diffstat (limited to 'service/http')
-rw-r--r-- | service/http/config.go | 17 | ||||
-rw-r--r-- | service/http/config_test.go | 4 | ||||
-rw-r--r-- | service/http/fcgi_test.go | 59 | ||||
-rw-r--r-- | service/http/service.go | 58 | ||||
-rw-r--r-- | service/http/service_test.go | 2 |
5 files changed, 129 insertions, 11 deletions
diff --git a/service/http/config.go b/service/http/config.go index ecd5a3a8..4b5950b3 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. @@ -47,11 +54,19 @@ type SSLConfig struct { Cert string } +func (c *Config) EnableHTTP() bool { + return c.Address != "" +} + // EnableTLS returns true if rr must listen TLS connections. 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 { @@ -146,7 +161,7 @@ func (c *Config) Valid() error { return err } - if !strings.Contains(c.Address, ":") { + if c.Address != "" && !strings.Contains(c.Address, ":") { return errors.New("mailformed http server address") } diff --git a/service/http/config_test.go b/service/http/config_test.go index 48651e16..54e5b27a 100644 --- a/service/http/config_test.go +++ b/service/http/config_test.go @@ -19,7 +19,7 @@ func Test_Config_Hydrate_Error1(t *testing.T) { cfg := &mockCfg{`{"enable": true}`} c := &Config{} - assert.Error(t, c.Hydrate(cfg)) + assert.NoError(t, c.Hydrate(cfg)) } func Test_Config_Hydrate_Error2(t *testing.T) { @@ -252,7 +252,7 @@ func Test_Config_DeadPool(t *testing.T) { func Test_Config_InvalidAddress(t *testing.T) { cfg := &Config{ - Address: "", + Address: "unexpected_address", MaxRequestSize: 1024, Uploads: &UploadsConfig{ Dir: os.TempDir(), diff --git a/service/http/fcgi_test.go b/service/http/fcgi_test.go new file mode 100644 index 00000000..6b6cd0f3 --- /dev/null +++ b/service/http/fcgi_test.go @@ -0,0 +1,59 @@ +package http + +import ( + "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus/hooks/test" + "github.com/spiral/roadrunner/service" + "github.com/stretchr/testify/assert" + "io/ioutil" + "net/http/httptest" + "testing" + "time" + "github.com/yookoala/gofast" +) + +func Test_FCGI_Service_Echo(t *testing.T) { + logger, _ := test.NewNullLogger() + logger.SetLevel(logrus.DebugLevel) + + c := service.NewContainer(logger) + c.Register(ID, &Service{}) + + assert.NoError(t, c.Init(&testCfg{httpCfg: `{ + "fcgi": { + "address": "tcp://0.0.0.0:6082" + }, + "workers":{ + "command": "php ../../tests/http/client.php echo pipes", + "pool": {"numWorkers": 1} + } + }`})) + + s, st := c.Get(ID) + assert.NotNil(t, s) + assert.Equal(t, service.StatusOK, st) + + // should do nothing + s.(*Service).Stop() + + go func() { assert.NoError(t, c.Serve()) }() + time.Sleep(time.Millisecond * 100) + defer c.Stop() + + fcgiConnFactory := gofast.SimpleConnFactory("tcp", "0.0.0.0:6082") + + fcgiHandler := gofast.NewHandler( + gofast.BasicParamsMap(gofast.BasicSession), + gofast.SimpleClientFactory(fcgiConnFactory, 0), + ) + + w := httptest.NewRecorder() + req := httptest.NewRequest("GET", "http://site.local/?hello=world", nil) + fcgiHandler.ServeHTTP(w, req) + + body, err := ioutil.ReadAll(w.Result().Body); + + assert.NoError(t, err) + assert.Equal(t, 201, w.Result().StatusCode) + assert.Equal(t, "WORLD", string(body)) +} 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 { diff --git a/service/http/service_test.go b/service/http/service_test.go index b3b001c7..69cb7003 100644 --- a/service/http/service_test.go +++ b/service/http/service_test.go @@ -53,7 +53,7 @@ func Test_Service_NoConfig(t *testing.T) { c := service.NewContainer(logger) c.Register(ID, &Service{}) - assert.Error(t, c.Init(&testCfg{httpCfg: `{"Enable":true}`})) + c.Init(&testCfg{httpCfg: `{"Enable":true}`}) s, st := c.Get(ID) assert.NotNil(t, s) |