summaryrefslogtreecommitdiff
path: root/service/static
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-07-08 13:06:05 -0700
committerWolfy-J <[email protected]>2018-07-08 13:06:05 -0700
commit29c9bf94350e86ec96f5ce5eeb476dfcd57302cd (patch)
tree9f59af6446958d144b7de91b5005a3727dc90661 /service/static
parent3c3a7801100f29c99a5e446646c818bf16ccd5f0 (diff)
dependency injection and lighter service Init methods.
Diffstat (limited to 'service/static')
-rw-r--r--service/static/config.go34
-rw-r--r--service/static/service.go30
2 files changed, 26 insertions, 38 deletions
diff --git a/service/static/config.go b/service/static/config.go
index 1020b8cd..6a2d1206 100644
--- a/service/static/config.go
+++ b/service/static/config.go
@@ -5,6 +5,7 @@ import (
"os"
"path"
"strings"
+ "github.com/spiral/roadrunner/service"
)
// Config describes file location and controls access to them.
@@ -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/service.go b/service/static/service.go
index add242e4..968cb594 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) (enabled bool, err 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