diff options
author | Wim <wim@42.be> | 2018-02-21 00:48:10 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2018-02-21 00:48:10 +0100 |
commit | 55ab0c12f12e2f0ee15dccd519e1fca4d011b73b (patch) | |
tree | 8d114d1129424e446a0124b9520d2ff3b2175369 /vendor/github.com/labstack/echo/middleware/rewrite.go | |
parent | d1227b5fc9de9f7a04fbf71292dd224aa8806411 (diff) | |
download | matterbridge-msglm-55ab0c12f12e2f0ee15dccd519e1fca4d011b73b.tar.gz matterbridge-msglm-55ab0c12f12e2f0ee15dccd519e1fca4d011b73b.tar.bz2 matterbridge-msglm-55ab0c12f12e2f0ee15dccd519e1fca4d011b73b.zip |
Update vendor labstack/echo
Diffstat (limited to 'vendor/github.com/labstack/echo/middleware/rewrite.go')
-rw-r--r-- | vendor/github.com/labstack/echo/middleware/rewrite.go | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/vendor/github.com/labstack/echo/middleware/rewrite.go b/vendor/github.com/labstack/echo/middleware/rewrite.go new file mode 100644 index 00000000..60a59bec --- /dev/null +++ b/vendor/github.com/labstack/echo/middleware/rewrite.go @@ -0,0 +1,83 @@ +package middleware + +import ( + "regexp" + "strings" + + "github.com/labstack/echo" +) + +type ( + // RewriteConfig defines the config for Rewrite middleware. + RewriteConfig struct { + // Skipper defines a function to skip middleware. + Skipper Skipper + + // Rules defines the URL path rewrite rules. The values captured in asterisk can be + // retrieved by index e.g. $1, $2 and so on. + // Example: + // "/old": "/new", + // "/api/*": "/$1", + // "/js/*": "/public/javascripts/$1", + // "/users/*/orders/*": "/user/$1/order/$2", + // Required. + Rules map[string]string `yaml:"rules"` + + rulesRegex map[*regexp.Regexp]string + } +) + +var ( + // DefaultRewriteConfig is the default Rewrite middleware config. + DefaultRewriteConfig = RewriteConfig{ + Skipper: DefaultSkipper, + } +) + +// Rewrite returns a Rewrite middleware. +// +// Rewrite middleware rewrites the URL path based on the provided rules. +func Rewrite(rules map[string]string) echo.MiddlewareFunc { + c := DefaultRewriteConfig + c.Rules = rules + return RewriteWithConfig(c) +} + +// RewriteWithConfig returns a Rewrite middleware with config. +// See: `Rewrite()`. +func RewriteWithConfig(config RewriteConfig) echo.MiddlewareFunc { + // Defaults + if config.Rules == nil { + panic("echo: rewrite middleware requires url path rewrite rules") + } + if config.Skipper == nil { + config.Skipper = DefaultBodyDumpConfig.Skipper + } + config.rulesRegex = map[*regexp.Regexp]string{} + + // Initialize + for k, v := range config.Rules { + k = strings.Replace(k, "*", "(\\S*)", -1) + config.rulesRegex[regexp.MustCompile(k)] = v + } + + return func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) (err error) { + if config.Skipper(c) { + return next(c) + } + + req := c.Request() + + // Rewrite + for k, v := range config.rulesRegex { + replacer := captureTokens(k, req.URL.Path) + if replacer != nil { + req.URL.Path = replacer.Replace(v) + } + } + + return next(c) + } + } +} |