diff options
author | Wolfy-J <[email protected]> | 2018-09-23 15:55:57 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2018-09-23 15:55:57 +0300 |
commit | 6628249e68a85e6c2fed6d5802fa247388b053dc (patch) | |
tree | def759b509dbd32569afc8229c8888fa6599e1bf /service/http | |
parent | bdff4b25d2a879357bc0ed53e96c0b551de07f88 (diff) | |
parent | eb64ebee3c77522202c5163513e7318bd630f8be (diff) |
Merge pull request #37 from spiral/feature/1.3.0
Feature/1.3.0
Diffstat (limited to 'service/http')
-rw-r--r-- | service/http/config.go | 21 | ||||
-rw-r--r-- | service/http/config_test.go | 16 | ||||
-rw-r--r-- | service/http/handler.go | 2 | ||||
-rw-r--r-- | service/http/handler_test.go | 36 | ||||
-rw-r--r-- | service/http/request.go | 2 | ||||
-rw-r--r-- | service/http/rpc_test.go | 6 | ||||
-rw-r--r-- | service/http/service.go | 25 | ||||
-rw-r--r-- | service/http/service_test.go | 43 | ||||
-rw-r--r-- | service/http/uploads_config.go | 6 | ||||
-rw-r--r-- | service/http/uploads_test.go | 8 |
10 files changed, 74 insertions, 91 deletions
diff --git a/service/http/config.go b/service/http/config.go index 5be42ae6..b11d807c 100644 --- a/service/http/config.go +++ b/service/http/config.go @@ -9,9 +9,6 @@ import ( // Config configures RoadRunner HTTP server. type Config struct { - // Enable enables http service. - Enable bool - // Address and port to handle as http server. Address string @@ -27,21 +24,27 @@ type Config struct { // 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 err := cfg.Unmarshal(c); err != nil { - return err + if c.Workers == nil { + c.Workers = &roadrunner.ServerConfig{} } - if !c.Enable { - return nil + if c.Uploads == nil { + c.Uploads = &UploadsConfig{} } - if err := c.Valid(); err != nil { + c.Uploads.InitDefaults() + c.Workers.InitDefaults() + + if err := cfg.Unmarshal(c); err != nil { return err } - c.Workers.SetDefaults() c.Workers.UpscaleDurations() + if err := c.Valid(); err != nil { + return err + } + return nil } diff --git a/service/http/config_test.go b/service/http/config_test.go index 2e3fe731..823efb32 100644 --- a/service/http/config_test.go +++ b/service/http/config_test.go @@ -31,7 +31,6 @@ func Test_Config_Hydrate_Error2(t *testing.T) { func Test_Config_Valid(t *testing.T) { cfg := &Config{ - Enable: true, Address: ":8080", MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -39,7 +38,7 @@ func Test_Config_Valid(t *testing.T) { Forbid: []string{".go"}, }, Workers: &roadrunner.ServerConfig{ - Command: "php php-src/tests/client.php echo pipes", + Command: "php tests/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -54,11 +53,10 @@ func Test_Config_Valid(t *testing.T) { func Test_Config_NoUploads(t *testing.T) { cfg := &Config{ - Enable: true, Address: ":8080", MaxRequest: 1024, Workers: &roadrunner.ServerConfig{ - Command: "php php-src/tests/client.php echo pipes", + Command: "php tests/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -73,7 +71,6 @@ func Test_Config_NoUploads(t *testing.T) { func Test_Config_NoWorkers(t *testing.T) { cfg := &Config{ - Enable: true, Address: ":8080", MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -87,7 +84,6 @@ func Test_Config_NoWorkers(t *testing.T) { func Test_Config_NoPool(t *testing.T) { cfg := &Config{ - Enable: true, Address: ":8080", MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -95,7 +91,7 @@ func Test_Config_NoPool(t *testing.T) { Forbid: []string{".go"}, }, Workers: &roadrunner.ServerConfig{ - Command: "php php-src/tests/client.php echo pipes", + Command: "php tests/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 0, @@ -110,7 +106,6 @@ func Test_Config_NoPool(t *testing.T) { func Test_Config_DeadPool(t *testing.T) { cfg := &Config{ - Enable: true, Address: ":8080", MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -118,7 +113,7 @@ func Test_Config_DeadPool(t *testing.T) { Forbid: []string{".go"}, }, Workers: &roadrunner.ServerConfig{ - Command: "php php-src/tests/client.php echo pipes", + Command: "php tests/client.php echo pipes", Relay: "pipes", }, } @@ -128,7 +123,6 @@ func Test_Config_DeadPool(t *testing.T) { func Test_Config_InvalidAddress(t *testing.T) { cfg := &Config{ - Enable: true, Address: "", MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -136,7 +130,7 @@ func Test_Config_InvalidAddress(t *testing.T) { Forbid: []string{".go"}, }, Workers: &roadrunner.ServerConfig{ - Command: "php php-src/tests/client.php echo pipes", + Command: "php tests/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, diff --git a/service/http/handler.go b/service/http/handler.go index 9e67e5b4..945cd51e 100644 --- a/service/http/handler.go +++ b/service/http/handler.go @@ -51,7 +51,7 @@ func (h *Handler) Listen(l func(event int, ctx interface{})) { h.lsn = l } -// middleware serve using PSR-7 requests passed to underlying application. Attempts to serve static files first if enabled. +// mdwr serve using PSR-7 requests passed to underlying application. Attempts to serve static files first if enabled. func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // validating request size if h.cfg.MaxRequest != 0 { diff --git a/service/http/handler_test.go b/service/http/handler_test.go index 3b51c873..e864b86e 100644 --- a/service/http/handler_test.go +++ b/service/http/handler_test.go @@ -39,7 +39,7 @@ func TestServer_Echo(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php echo pipes", + Command: "php ../../tests/http/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -74,7 +74,7 @@ func Test_HandlerErrors(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php echo pipes", + Command: "php ../../tests/http/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -101,7 +101,7 @@ func Test_Handler_JSON_error(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php echo pipes", + Command: "php ../../tests/http/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -130,7 +130,7 @@ func TestServer_Headers(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php header pipes", + Command: "php ../../tests/http/client.php header pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -177,7 +177,7 @@ func TestServer_Cookies(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php cookie pipes", + Command: "php ../../tests/http/client.php cookie pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -228,7 +228,7 @@ func TestServer_JsonPayload_POST(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php payload pipes", + Command: "php ../../tests/http/client.php payload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -278,7 +278,7 @@ func TestServer_JsonPayload_PUT(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php payload pipes", + Command: "php ../../tests/http/client.php payload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -324,7 +324,7 @@ func TestServer_JsonPayload_PATCH(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php payload pipes", + Command: "php ../../tests/http/client.php payload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -370,7 +370,7 @@ func TestServer_FormData_POST(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php data pipes", + Command: "php ../../tests/http/client.php data pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -428,7 +428,7 @@ func TestServer_FormData_PUT(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php data pipes", + Command: "php ../../tests/http/client.php data pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -486,7 +486,7 @@ func TestServer_FormData_PATCH(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php data pipes", + Command: "php ../../tests/http/client.php data pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -544,7 +544,7 @@ func TestServer_Multipart_POST(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php data pipes", + Command: "php ../../tests/http/client.php data pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -606,7 +606,7 @@ func TestServer_Multipart_PUT(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php data pipes", + Command: "php ../../tests/http/client.php data pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -668,7 +668,7 @@ func TestServer_Multipart_PATCH(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php data pipes", + Command: "php ../../tests/http/client.php data pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -730,7 +730,7 @@ func TestServer_Error(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php error pipes", + Command: "php ../../tests/http/client.php error pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -764,7 +764,7 @@ func TestServer_Error2(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php error2 pipes", + Command: "php ../../tests/http/client.php error2 pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -798,7 +798,7 @@ func TestServer_Error3(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php pid pipes", + Command: "php ../../tests/http/client.php pid pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -843,7 +843,7 @@ func BenchmarkHandler_Listen_Echo(b *testing.B) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php echo pipes", + Command: "php ../../tests/http/client.php echo pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: int64(runtime.NumCPU()), diff --git a/service/http/request.go b/service/http/request.go index 531a1efd..d733b20c 100644 --- a/service/http/request.go +++ b/service/http/request.go @@ -49,7 +49,7 @@ type Request struct { // Uploads contains list of uploaded files, their names, sized and associations with temporary files. Uploads *Uploads `json:"uploads"` - // Attributes can be set by chained middleware to safely pass value from Golang to PHP. See: GetAttribute, SetAttribute functions. + // Attributes can be set by chained mdwr to safely pass value from Golang to PHP. See: GetAttribute, SetAttribute functions. Attributes map[string]interface{} `json:"attributes"` // request body can be parsedData or []byte diff --git a/service/http/rpc_test.go b/service/http/rpc_test.go index 32bb776c..ba3efd2e 100644 --- a/service/http/rpc_test.go +++ b/service/http/rpc_test.go @@ -33,7 +33,7 @@ func Test_RPC(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php pid pipes", + "command": "php ../../tests/http/client.php pid pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -94,7 +94,7 @@ func Test_RPC_Unix(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php pid pipes", + "command": "php ../../tests/http/client.php pid pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -148,7 +148,7 @@ func Test_Workers(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php pid pipes", + "command": "php ../../tests/http/client.php pid pipes", "relay": "pipes", "pool": { "numWorkers": 1, diff --git a/service/http/service.go b/service/http/service.go index ecce1c15..bb75a2c0 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -27,7 +27,7 @@ type Service struct { cfg *Config env env.Environment lsns []func(event int, ctx interface{}) - mdws []middleware + mdwr []middleware mu sync.Mutex rr *roadrunner.Server stopping int32 @@ -35,9 +35,9 @@ type Service struct { http *http.Server } -// AddMiddleware adds new net/http middleware. +// AddMiddleware adds new net/http mdwr. func (s *Service) AddMiddleware(m middleware) { - s.mdws = append(s.mdws, m) + s.mdwr = append(s.mdwr, m) } // AddListener attaches server event watcher. @@ -48,10 +48,6 @@ func (s *Service) AddListener(l func(event int, ctx interface{})) { // Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of // misconfiguration. Services must not be used without proper configuration pushed first. func (s *Service) Init(cfg *Config, r *rpc.Service, e env.Environment) (bool, error) { - if !cfg.Enable { - return false, nil - } - s.cfg = cfg s.env = e if r != nil { @@ -87,17 +83,14 @@ func (s *Service) Serve() error { s.rr.Listen(s.listener) s.srv.Listen(s.listener) - if len(s.mdws) == 0 { - s.http.Handler = s.srv - } else { - s.http.Handler = s - } + s.http.Handler = s + s.mu.Unlock() if err := rr.Start(); err != nil { return err } - defer s.rr.Stop() + defer rr.Stop() return s.http.ListenAndServe() } @@ -118,13 +111,13 @@ func (s *Service) Stop() { s.http.Shutdown(context.Background()) } -// middleware handles connection using set of mdws and rr PSR-7 server. +// mdwr handles connection using set of mdwr and rr PSR-7 server. func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) { r = attributes.Init(r) - // chaining middlewares + // chaining mdwr f := s.srv.ServeHTTP - for _, m := range s.mdws { + for _, m := range s.mdwr { f = m(f) } f(w, r) diff --git a/service/http/service_test.go b/service/http/service_test.go index 8dab7cd4..d1d601dc 100644 --- a/service/http/service_test.go +++ b/service/http/service_test.go @@ -25,6 +25,10 @@ type testCfg struct { func (cfg *testCfg) Get(name string) service.Config { if name == ID { + if cfg.httpCfg == "" { + return nil + } + return &testCfg{target: cfg.httpCfg} } @@ -63,24 +67,7 @@ func Test_Service_Configure_Disable(t *testing.T) { c := service.NewContainer(logger) c.Register(ID, &Service{}) - assert.NoError(t, c.Init(&testCfg{httpCfg: `{ - "enable": false, - "address": ":8070", - "maxRequest": 1024, - "uploads": { - "dir": ` + tmpDir() + `, - "forbid": [] - }, - "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", - "relay": "pipes", - "pool": { - "numWorkers": 1, - "allocateTimeout": 10000000, - "destroyTimeout": 10000000 - } - } - }`})) + assert.NoError(t, c.Init(&testCfg{})) s, st := c.Get(ID) assert.NotNil(t, s) @@ -103,7 +90,7 @@ func Test_Service_Configure_Enable(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -134,7 +121,7 @@ func Test_Service_Echo(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -187,7 +174,7 @@ func Test_Service_Env(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php env pipes", + "command": "php ../../tests/http/client.php env pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -239,7 +226,7 @@ func Test_Service_ErrorEcho(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echoerr pipes", + "command": "php ../../tests/http/client.php echoerr pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -299,7 +286,7 @@ func Test_Service_Middleware(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -373,7 +360,7 @@ func Test_Service_Listener(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -417,7 +404,7 @@ func Test_Service_Error(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php echo pipes", + "command": "php ../../tests/http/client.php echo pipes", "relay": "---", "pool": { "numWorkers": 1, @@ -446,7 +433,7 @@ func Test_Service_Error2(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php broken pipes", + "command": "php ../../tests/http/client.php broken pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -475,7 +462,7 @@ func Test_Service_Error3(t *testing.T) { "forbid": [] }, "workers" - "command": "php ../../php-src/tests/http/client.php broken pipes", + "command": "php ../../tests/http/client.php broken pipes", "relay": "pipes", "pool": { "numWorkers": 1, @@ -502,7 +489,7 @@ func Test_Service_Error4(t *testing.T) { "forbid": [] }, "workers":{ - "command": "php ../../php-src/tests/http/client.php broken pipes", + "command": "php ../../tests/http/client.php broken pipes", "relay": "pipes", "pool": { "numWorkers": 1, diff --git a/service/http/uploads_config.go b/service/http/uploads_config.go index e90d9b70..3f655064 100644 --- a/service/http/uploads_config.go +++ b/service/http/uploads_config.go @@ -16,6 +16,12 @@ type UploadsConfig struct { Forbid []string } +// InitDefaults sets missing values to their default values. +func (cfg *UploadsConfig) InitDefaults() error { + cfg.Forbid = []string{".php", ".exe", ".bat"} + return nil +} + // TmpDir returns temporary directory. func (cfg *UploadsConfig) TmpDir() string { if cfg.Dir != "" { diff --git a/service/http/uploads_test.go b/service/http/uploads_test.go index b2662bf7..96e95733 100644 --- a/service/http/uploads_test.go +++ b/service/http/uploads_test.go @@ -27,7 +27,7 @@ func TestServer_Upload_File(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php upload pipes", + Command: "php ../../tests/http/client.php upload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -88,7 +88,7 @@ func TestServer_Upload_NestedFile(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php upload pipes", + Command: "php ../../tests/http/client.php upload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -149,7 +149,7 @@ func TestServer_Upload_File_NoTmpDir(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php upload pipes", + Command: "php ../../tests/http/client.php upload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, @@ -210,7 +210,7 @@ func TestServer_Upload_File_Forbids(t *testing.T) { }, }, rr: roadrunner.NewServer(&roadrunner.ServerConfig{ - Command: "php ../../php-src/tests/http/client.php upload pipes", + Command: "php ../../tests/http/client.php upload pipes", Relay: "pipes", Pool: &roadrunner.Config{ NumWorkers: 1, |