diff options
Diffstat (limited to 'service/static')
-rw-r--r-- | service/static/config.go | 34 | ||||
-rw-r--r-- | service/static/config_test.go | 21 | ||||
-rw-r--r-- | service/static/service.go | 30 |
3 files changed, 47 insertions, 38 deletions
diff --git a/service/static/config.go b/service/static/config.go index 1020b8cd..95fdbeee 100644 --- a/service/static/config.go +++ b/service/static/config.go @@ -2,6 +2,7 @@ package static import ( "github.com/pkg/errors" + "github.com/spiral/roadrunner/service" "os" "path" "strings" @@ -20,22 +21,18 @@ type Config struct { Forbid []string } -// Forbids must return true if file extension is not allowed for the upload. -func (cfg *Config) Forbids(filename string) bool { - ext := strings.ToLower(path.Ext(filename)) - - for _, v := range cfg.Forbid { - if ext == v { - return true - } +// 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 } - return false + return c.Valid() } -// Valid validates existence of directory. -func (cfg *Config) Valid() error { - st, err := os.Stat(cfg.Dir) +// Valid returns nil if config is valid. +func (c *Config) Valid() error { + st, err := os.Stat(c.Dir) if err != nil { if os.IsNotExist(err) { return errors.New("root directory does not exists") @@ -50,3 +47,16 @@ func (cfg *Config) Valid() error { return nil } + +// Forbids must return true if file extension is not allowed for the upload. +func (c *Config) Forbids(filename string) bool { + ext := strings.ToLower(path.Ext(filename)) + + for _, v := range c.Forbid { + if ext == v { + return true + } + } + + return false +} diff --git a/service/static/config_test.go b/service/static/config_test.go index d2099cdf..18168d59 100644 --- a/service/static/config_test.go +++ b/service/static/config_test.go @@ -1,10 +1,31 @@ package static import ( + "encoding/json" + "github.com/spiral/roadrunner/service" "github.com/stretchr/testify/assert" "testing" ) +type mockCfg struct{ cfg string } + +func (cfg *mockCfg) Get(name string) service.Config { return nil } +func (cfg *mockCfg) Unmarshal(out interface{}) error { return json.Unmarshal([]byte(cfg.cfg), out) } + +func Test_Config_Hydrate(t *testing.T) { + cfg := &mockCfg{`{"dir": "./"}`} + c := &Config{} + + assert.NoError(t, c.Hydrate(cfg)) +} + +func Test_Config_Hydrate_Error(t *testing.T) { + cfg := &mockCfg{`{"dir": "/dir/"}`} + c := &Config{} + + assert.Error(t, c.Hydrate(cfg)) +} + func TestConfig_Forbids(t *testing.T) { cfg := Config{Forbid: []string{".php"}} diff --git a/service/static/service.go b/service/static/service.go index add242e4..98d8313c 100644 --- a/service/static/service.go +++ b/service/static/service.go @@ -1,7 +1,6 @@ package static import ( - "github.com/spiral/roadrunner/service" rrttp "github.com/spiral/roadrunner/service/http" "net/http" "path" @@ -22,39 +21,18 @@ type Service struct { // Init must return configure service and return true if service hasStatus enabled. Must return error in case of // misconfiguration. Services must not be used without proper configuration pushed first. -func (s *Service) Init(cfg service.Config, c service.Container) (enabled bool, err error) { - config := &Config{} - if err := cfg.Unmarshal(config); err != nil { - return false, err - } - - if !config.Enable { +func (s *Service) Init(cfg *Config, r *rrttp.Service) (bool, error) { + if !cfg.Enable || r == nil { return false, nil } - if err := config.Valid(); err != nil { - return false, err - } - - s.cfg = config + s.cfg = cfg s.root = http.Dir(s.cfg.Dir) - - // registering as middleware - if h, ok := c.Get(rrttp.ID); ok >= service.StatusConfigured { - if h, ok := h.(*rrttp.Service); ok { - h.AddMiddleware(s.middleware) - } - } + r.AddMiddleware(s.middleware) return true, nil } -// Serve serves the service. -func (s *Service) Serve() error { return nil } - -// Stop stops the service. -func (s *Service) Stop() {} - // middleware must return true if request/response pair is handled within the middleware. func (s *Service) middleware(f http.HandlerFunc) http.HandlerFunc { // Define the http.HandlerFunc |