diff options
author | Wolfy-J <[email protected]> | 2018-06-13 23:46:13 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2018-06-13 23:46:13 +0300 |
commit | c3ccb29fe412baa8c4b02a1630f3a4a040ab722c (patch) | |
tree | 7a61db95d8e4d02ac5740d593ed708358f34949a /service/static/service.go | |
parent | 50f820833eeef8518b3b978b33c6f20391225162 (diff) | |
parent | 8ab8c64413ded038e3c8816647209c3b961b3a35 (diff) |
Merge pull request #9 from spiral/develop
HTTP
Diffstat (limited to 'service/static/service.go')
-rw-r--r-- | service/static/service.go | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/service/static/service.go b/service/static/service.go new file mode 100644 index 00000000..43891aa8 --- /dev/null +++ b/service/static/service.go @@ -0,0 +1,89 @@ +package static + +import ( + "net/http" + "path" + "strings" + rrttp "github.com/spiral/roadrunner/service/http" + "github.com/spiral/roadrunner/service" +) + +// ID contains default service name. +const ID = "static" + +// Service serves static files. Potentially convert into middleware? +type Service struct { + // server configuration (location, forbidden files and etc) + cfg *Config + + // root is initiated http directory + root http.Dir +} + +// 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 { + return false, nil + } + + if err := config.Valid(); err != nil { + return false, err + } + + s.cfg = config + 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) + } + } + + 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 withing the middleware. +func (s *Service) middleware(w http.ResponseWriter, r *http.Request) bool { + fPath := r.URL.Path + + if !strings.HasPrefix(fPath, "/") { + fPath = "/" + fPath + } + fPath = path.Clean(fPath) + + if s.cfg.Forbids(fPath) { + return false + } + + f, err := s.root.Open(fPath) + if err != nil { + return false + } + defer f.Close() + + d, err := f.Stat() + if err != nil { + return false + } + + // do not serve directories + if d.IsDir() { + return false + } + + http.ServeContent(w, r, d.Name(), d.ModTime(), f) + return true +} |