Skip to content

Commit

Permalink
Fixed #51, Closes #52
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vr@labstack.com>
  • Loading branch information
vishr committed Jun 17, 2019
1 parent fa17c18 commit 90a6ad9
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 102 deletions.
26 changes: 23 additions & 3 deletions armor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/labstack/armor/admin"
"io/ioutil"
stdLog "log"
"net"
Expand All @@ -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"
Expand Down
4 changes: 0 additions & 4 deletions plugin/body_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 0 additions & 4 deletions plugin/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 0 additions & 4 deletions plugin/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 0 additions & 4 deletions plugin/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 0 additions & 4 deletions plugin/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 0 additions & 4 deletions plugin/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
76 changes: 53 additions & 23 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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, "${", "}")}
}
Expand Down
4 changes: 0 additions & 4 deletions plugin/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 0 additions & 24 deletions plugin/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 0 additions & 4 deletions plugin/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 0 additions & 4 deletions plugin/secure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 0 additions & 8 deletions plugin/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
Loading

0 comments on commit 90a6ad9

Please sign in to comment.