diff options
author | Dmitry Patsura <[email protected]> | 2019-06-13 21:03:15 +0300 |
---|---|---|
committer | Dmitry Patsura <[email protected]> | 2019-06-13 21:03:15 +0300 |
commit | a612f9d4ac8d2f5659a2e984067baf5a11fd6c2a (patch) | |
tree | c788a86b155b9f99c2a5d388e62fb89a99da58f6 /service | |
parent | 4d3b5a3fdd7750e7b6389fc7b4c92d56226e941e (diff) |
Feature: Allow to disable http, and use only FastCGI
Diffstat (limited to 'service')
-rw-r--r-- | service/http/config.go | 6 | ||||
-rw-r--r-- | service/http/config_test.go | 4 | ||||
-rw-r--r-- | service/http/fcgi_test.go | 1 | ||||
-rw-r--r-- | service/http/service.go | 23 | ||||
-rw-r--r-- | service/http/service_test.go | 2 |
5 files changed, 23 insertions, 13 deletions
diff --git a/service/http/config.go b/service/http/config.go index 585216d5..4b5950b3 100644 --- a/service/http/config.go +++ b/service/http/config.go @@ -54,6 +54,10 @@ 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 != "" @@ -157,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 index d053ce03..6b6cd0f3 100644 --- a/service/http/fcgi_test.go +++ b/service/http/fcgi_test.go @@ -20,7 +20,6 @@ func Test_FCGI_Service_Echo(t *testing.T) { c.Register(ID, &Service{}) assert.NoError(t, c.Init(&testCfg{httpCfg: `{ - "address": ":6029", "fcgi": { "address": "tcp://0.0.0.0:6082" }, diff --git a/service/http/service.go b/service/http/service.go index 967aa63b..b309cd45 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -69,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 } @@ -94,7 +98,9 @@ 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() @@ -113,9 +119,11 @@ func (s *Service) Serve() error { err := make(chan error, 3) - go func() { - err <- s.http.ListenAndServe() - }() + if s.http != nil { + go func() { + err <- s.http.ListenAndServe() + }() + } if s.https != nil { go func() { @@ -136,9 +144,6 @@ 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()) @@ -148,7 +153,9 @@ func (s *Service) Stop() { 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). 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) |