From 90a6ad99afd235b94bdf596f9cd4917732ae73e3 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Mon, 17 Jun 2019 16:42:12 -0700 Subject: [PATCH] Fixed #51, Closes #52 Signed-off-by: Vishal Rana --- armor.go | 26 +++++++++++++-- cmd/root.go | 2 +- plugin/body_limit.go | 4 --- plugin/cors.go | 4 --- plugin/file.go | 4 --- plugin/gzip.go | 4 --- plugin/header.go | 4 --- plugin/logger.go | 4 --- plugin/plugin.go | 76 ++++++++++++++++++++++++++++++-------------- plugin/proxy.go | 4 --- plugin/redirect.go | 24 -------------- plugin/rewrite.go | 4 --- plugin/secure.go | 4 --- plugin/slash.go | 8 ----- plugin/static.go | 4 --- store/postgres.go | 3 +- store/store.go | 4 ++- store/storm.go | 2 +- 18 files changed, 83 insertions(+), 102 deletions(-) diff --git a/armor.go b/armor.go index d1a7b3a..ebd6e2f 100644 --- a/armor.go +++ b/armor.go @@ -100,6 +100,20 @@ const ( Website = "https://armor.labstack.com" ) +var ( + prePlugins = map[string]bool{ + plugin.PluginLogger: true, + plugin.PluginRedirect: true, + plugin.PluginHTTPSRedirect: true, + plugin.PluginHTTPSWWWRedirect: true, + plugin.PluginHTTPSNonWWWRedirect: true, + plugin.PluginWWWRedirect: true, + plugin.PluginAddTrailingSlash: true, + plugin.PluginRemoveTrailingSlash: true, + plugin.PluginNonWWWRedirect: true, + } +) + func (a *Armor) FindHost(name string, add bool) (h *Host) { a.mutex.Lock() defer a.mutex.Unlock() @@ -130,7 +144,7 @@ func (a *Armor) FindHost(name string, add bool) (h *Host) { func (a *Armor) AddPlugin(p plugin.Plugin) { a.mutex.Lock() defer a.mutex.Unlock() - if p.Priority() < 0 { + if p.Order() < 0 { a.Echo.Pre(p.Process) } else { a.Echo.Use(p.Process) @@ -222,14 +236,20 @@ func (a *Armor) SavePlugins() { } // Save + i, j := -50, 0 for _, p := range plugins { p.Source = store.File p.ID = util.ID() now := time.Now() p.CreatedAt = now p.UpdatedAt = now - - // Insert + if _, ok := prePlugins[p.Name]; ok { + i++ + p.Order = i + } else { + j++ + p.Order = j + } if err := a.Store.AddPlugin(p); err != nil { panic(err) } diff --git a/cmd/root.go b/cmd/root.go index d46b21f..17bb136 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/labstack/armor/admin" "io/ioutil" stdLog "log" "net" @@ -10,7 +11,6 @@ import ( "github.com/ghodss/yaml" "github.com/labstack/armor" - "github.com/labstack/armor/admin" "github.com/labstack/armor/store" "github.com/labstack/gommon/color" "github.com/labstack/gommon/log" diff --git a/plugin/body_limit.go b/plugin/body_limit.go index 5d7ac3c..05077a2 100644 --- a/plugin/body_limit.go +++ b/plugin/body_limit.go @@ -23,10 +23,6 @@ func (b *BodyLimit) Update(p Plugin) { b.Initialize() } -func (*BodyLimit) Priority() int { - return 1 -} - func (b *BodyLimit) Process(next echo.HandlerFunc) echo.HandlerFunc { b.mutex.RLock() defer b.mutex.RUnlock() diff --git a/plugin/cors.go b/plugin/cors.go index 0f5a970..28b912f 100644 --- a/plugin/cors.go +++ b/plugin/cors.go @@ -16,10 +16,6 @@ func (c *CORS) Initialize() { c.Middleware = middleware.CORSWithConfig(c.CORSConfig) } -func (*CORS) Priority() int { - return 1 -} - func (c *CORS) Update(p Plugin) { c.mutex.Lock() defer c.mutex.Unlock() diff --git a/plugin/file.go b/plugin/file.go index 14b8410..f16b4ce 100644 --- a/plugin/file.go +++ b/plugin/file.go @@ -23,10 +23,6 @@ func (f *File) Update(p Plugin) { f.Initialize() } -func (*File) Priority() int { - return 1 -} - func (f *File) Process(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { f.mutex.RLock() diff --git a/plugin/gzip.go b/plugin/gzip.go index 29b1042..6106dfc 100644 --- a/plugin/gzip.go +++ b/plugin/gzip.go @@ -23,10 +23,6 @@ func (g *Gzip) Update(p Plugin) { g.Initialize() } -func (*Gzip) Priority() int { - return 1 -} - func (g *Gzip) Process(next echo.HandlerFunc) echo.HandlerFunc { g.mutex.RLock() defer g.mutex.RUnlock() diff --git a/plugin/header.go b/plugin/header.go index a0c0200..493c1df 100644 --- a/plugin/header.go +++ b/plugin/header.go @@ -27,10 +27,6 @@ func (h *Header) Update(p Plugin) { h.Initialize() } -func (*Header) Priority() int { - return 1 -} - func (h *Header) Process(next echo.HandlerFunc) echo.HandlerFunc { h.mutex.RLock() defer h.mutex.RUnlock() diff --git a/plugin/logger.go b/plugin/logger.go index 7dc62f6..8ac5a03 100644 --- a/plugin/logger.go +++ b/plugin/logger.go @@ -23,10 +23,6 @@ func (l *Logger) Update(p Plugin) { l.Initialize() } -func (*Logger) Priority() int { - return -1 -} - func (l *Logger) Process(next echo.HandlerFunc) echo.HandlerFunc { l.mutex.RLock() defer l.mutex.RUnlock() diff --git a/plugin/plugin.go b/plugin/plugin.go index 0178743..728bd8e 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -21,15 +21,16 @@ type ( Initialize() Update(Plugin) Process(echo.HandlerFunc) echo.HandlerFunc - Priority() int + Order() int } RawPlugin map[string]interface{} // Base defines the base struct for plugins. Base struct { - name string mutex *sync.RWMutex + name string + order int // TODO: to disable Skip string `yaml:"skip"` Middleware echo.MiddlewareFunc `yaml:"-"` @@ -46,47 +47,69 @@ type ( } ) +const ( + // Plugin types + PluginBodyLimit = "body-limit" + PluginLogger = "logger" + PluginRedirect = "redirect" + PluginHTTPSRedirect = "https-redirect" + PluginHTTPSWWWRedirect = "https-www-redirect" + PluginHTTPSNonWWWRedirect = "https-non-www-redirect" + PluginWWWRedirect = "www-redirect" + PluginNonWWWRedirect = "non-www-redirect" + PluginAddTrailingSlash = "add-trailing-slash" + PluginRemoveTrailingSlash = "remove-trailing-slash" + PluginRewrite = "rewrite" + PluginSecure = "secure" + PluginCORS = "cors" + PluginGzip = "gzip" + PluginHeader = "header" + PluginProxy = "proxy" + PluginStatic = "static" + PluginFile = "file" +) + var ( bufferPool sync.Pool // DefaultLookup function DefaultLookup = func(base Base) (p Plugin) { switch base.Name() { - case "body-limit": + case PluginBodyLimit: p = &BodyLimit{Base: base} - case "logger": + case PluginLogger: p = &Logger{Base: base} - case "redirect": + case PluginRedirect: p = &Redirect{Base: base} - case "https-redirect": + case PluginHTTPSRedirect: p = &HTTPSRedirect{Base: base} - case "https-www-redirect": + case PluginHTTPSWWWRedirect: p = &HTTPSWWWRedirect{Base: base} - case "https-non-www-redirect": + case PluginHTTPSNonWWWRedirect: p = &HTTPSNonWWWRedirect{Base: base} - case "www-redirect": + case PluginWWWRedirect: p = &WWWRedirect{Base: base} - case "non-www-redirect": + case PluginNonWWWRedirect: p = &NonWWWRedirect{Base: base} - case "add-trailing-slash": + case PluginAddTrailingSlash: p = &AddTrailingSlash{Base: base} - case "remove-trailing-slash": + case PluginRemoveTrailingSlash: p = &RemoveTrailingSlash{Base: base} - case "rewrite": + case PluginRewrite: p = &Rewrite{Base: base} - case "secure": + case PluginSecure: p = &Secure{Base: base} - case "cors": + case PluginCORS: p = &CORS{Base: base} - case "gzip": + case PluginGzip: p = &Gzip{Base: base} - case "header": + case PluginHeader: p = &Header{Base: base} - case "proxy": + case PluginProxy: p = &Proxy{Base: base} - case "static": + case PluginStatic: p = &Static{Base: base} - case "file": + case PluginFile: p = &File{Base: base} } return @@ -106,9 +129,11 @@ func init() { } func (rp RawPlugin) Name() string { - name := rp["name"].(string) - delete(rp, "name") - return name + return rp["name"].(string) +} + +func (rp RawPlugin) Order() int { + return rp["order"].(int) } func (rp RawPlugin) JSON() []byte { @@ -124,6 +149,7 @@ func Decode(r RawPlugin, e *echo.Echo, l *log.Logger) (p Plugin) { name := r.Name() base := Base{ name: name, + order: r.Order(), mutex: new(sync.RWMutex), Skip: "false", Echo: e, @@ -147,6 +173,10 @@ func (b *Base) Name() string { return b.name } +func (b *Base) Order() int { + return b.order +} + func NewTemplate(t string) *Template { return &Template{Template: fasttemplate.New(t, "${", "}")} } diff --git a/plugin/proxy.go b/plugin/proxy.go index beccfb5..1067e24 100644 --- a/plugin/proxy.go +++ b/plugin/proxy.go @@ -64,10 +64,6 @@ func (p *Proxy) Update(plugin Plugin) { p.Initialize() } -func (*Proxy) Priority() int { - return 1 -} - func (p *Proxy) Process(next echo.HandlerFunc) echo.HandlerFunc { p.mutex.RLock() defer p.mutex.RUnlock() diff --git a/plugin/redirect.go b/plugin/redirect.go index 4795f1c..5b26a7a 100644 --- a/plugin/redirect.go +++ b/plugin/redirect.go @@ -67,10 +67,6 @@ func (r *Redirect) Update(p Plugin) { r.Initialize() } -func (*Redirect) Priority() int { - return -1 -} - func (r *Redirect) Process(next echo.HandlerFunc) echo.HandlerFunc { r.mutex.RLock() defer r.mutex.RUnlock() @@ -90,10 +86,6 @@ func (r *HTTPSRedirect) Update(p Plugin) { r.Initialize() } -func (*HTTPSRedirect) Priority() int { - return -1 -} - func (r *HTTPSRedirect) Process(next echo.HandlerFunc) echo.HandlerFunc { r.mutex.RLock() defer r.mutex.RUnlock() @@ -111,10 +103,6 @@ func (r *HTTPSWWWRedirect) Update(p Plugin) { r.Initialize() } -func (*HTTPSWWWRedirect) Priority() int { - return -1 -} - func (r *HTTPSWWWRedirect) Process(next echo.HandlerFunc) echo.HandlerFunc { r.mutex.RLock() defer r.mutex.RUnlock() @@ -140,10 +128,6 @@ func (r *HTTPSNonWWWRedirect) Update(p Plugin) { r.Initialize() } -func (*HTTPSNonWWWRedirect) Priority() int { - return -1 -} - func (r *HTTPSNonWWWRedirect) Process(next echo.HandlerFunc) echo.HandlerFunc { r.mutex.RLock() defer r.mutex.RUnlock() @@ -161,10 +145,6 @@ func (r *WWWRedirect) Update(p Plugin) { r.Initialize() } -func (*WWWRedirect) Priority() int { - return -1 -} - func (r *WWWRedirect) Process(next echo.HandlerFunc) echo.HandlerFunc { r.mutex.RLock() defer r.mutex.RUnlock() @@ -182,10 +162,6 @@ func (r *NonWWWRedirect) Update(p Plugin) { r.Initialize() } -func (*NonWWWRedirect) Priority() int { - return -1 -} - func (r *NonWWWRedirect) Process(next echo.HandlerFunc) echo.HandlerFunc { r.mutex.RLock() defer r.mutex.RUnlock() diff --git a/plugin/rewrite.go b/plugin/rewrite.go index e12ed8e..587fb13 100644 --- a/plugin/rewrite.go +++ b/plugin/rewrite.go @@ -23,10 +23,6 @@ func (r *Rewrite) Update(p Plugin) { r.Initialize() } -func (*Rewrite) Priority() int { - return 1 -} - func (r *Rewrite) Process(next echo.HandlerFunc) echo.HandlerFunc { r.mutex.RLock() defer r.mutex.RUnlock() diff --git a/plugin/secure.go b/plugin/secure.go index e4d8b2d..cbcde87 100644 --- a/plugin/secure.go +++ b/plugin/secure.go @@ -23,10 +23,6 @@ func (s *Secure) Update(p Plugin) { s.Initialize() } -func (*Secure) Priority() int { - return 1 -} - func (s *Secure) Process(next echo.HandlerFunc) echo.HandlerFunc { s.mutex.RLock() defer s.mutex.RUnlock() diff --git a/plugin/slash.go b/plugin/slash.go index 018cb5e..4adc6ee 100644 --- a/plugin/slash.go +++ b/plugin/slash.go @@ -28,10 +28,6 @@ func (s *AddTrailingSlash) Update(p Plugin) { s.Initialize() } -func (*AddTrailingSlash) Priority() int { - return -1 -} - func (s *AddTrailingSlash) Process(next echo.HandlerFunc) echo.HandlerFunc { s.mutex.RLock() defer s.mutex.RUnlock() @@ -49,10 +45,6 @@ func (s *RemoveTrailingSlash) Update(p Plugin) { s.Initialize() } -func (*RemoveTrailingSlash) Priority() int { - return -1 -} - func (s *RemoveTrailingSlash) Process(next echo.HandlerFunc) echo.HandlerFunc { s.mutex.RLock() defer s.mutex.RUnlock() diff --git a/plugin/static.go b/plugin/static.go index cebbd56..5b26498 100644 --- a/plugin/static.go +++ b/plugin/static.go @@ -23,10 +23,6 @@ func (s *Static) Update(p Plugin) { s.Initialize() } -func (*Static) Priority() int { - return 1 -} - func (s *Static) Process(next echo.HandlerFunc) echo.HandlerFunc { s.mutex.RLock() defer s.mutex.RUnlock() diff --git a/store/postgres.go b/store/postgres.go index 89a420d..3460921 100644 --- a/store/postgres.go +++ b/store/postgres.go @@ -53,7 +53,8 @@ func (pg *Postgres) FindPlugin(id string) (p *Plugin, err error) { } } p.Raw = plugin.RawPlugin{ - "name": p.Name, + "name": p.Name, + "order": p.Order, } err = json.Unmarshal(p.Config, &p.Raw) return diff --git a/store/store.go b/store/store.go index e50516f..6d250ae 100644 --- a/store/store.go +++ b/store/store.go @@ -22,6 +22,7 @@ type ( Plugin struct { ID string `json:"id" db:"id" storm:"id"` Name string `json:"name" db:"name"` + Order int `json:"order" db:"order"` Host string `json:"host" db:"host"` Path string `json:"path" db:"path"` Config types.JSONText `json:"config" db:"config"` @@ -44,7 +45,8 @@ const ( func decodeRawPlugin(plugins []*Plugin) (err error) { for _, p := range plugins { p.Raw = plugin.RawPlugin{ - "name": p.Name, + "name": p.Name, + "order": p.Order, } if err = json.Unmarshal(p.Config, &p.Raw); err != nil { return diff --git a/store/storm.go b/store/storm.go index c5cc10e..dc5bb31 100644 --- a/store/storm.go +++ b/store/storm.go @@ -33,7 +33,7 @@ func (s *Storm) FindPlugin(id string) (p *Plugin, err error) { func (s *Storm) FindPlugins() (plugins []*Plugin, err error) { plugins = []*Plugin{} - if err = s.All(&plugins); err != nil { + if err = s.Select().OrderBy("Order").Find(&plugins); err != nil { return } return plugins, decodeRawPlugin(plugins)