mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-06 03:31:00 +00:00
Add generic path rewrite middleware
And fix a bug in the logging middleware.
This commit is contained in:
@@ -14,9 +14,10 @@ import (
|
||||
var Logging = Func(func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
begin := time.Now()
|
||||
uri := r.RequestURI // capture the URI before running next, as it may get rewritten
|
||||
i := &interceptor{ResponseWriter: w, statusCode: http.StatusOK}
|
||||
next.ServeHTTP(i, r)
|
||||
log.Infof("%s %s (%d) %s", r.Method, r.RequestURI, i.statusCode, time.Since(begin))
|
||||
log.Infof("%s %s (%d) %s", r.Method, uri, i.statusCode, time.Since(begin))
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
26
common/middleware/path_rewrite.go
Normal file
26
common/middleware/path_rewrite.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// PathRewrite supports regex matching and replace on Request URIs
|
||||
func PathRewrite(regexp *regexp.Regexp, replacement string) Interface {
|
||||
return pathRewrite{
|
||||
regexp: regexp,
|
||||
replacement: replacement,
|
||||
}
|
||||
}
|
||||
|
||||
type pathRewrite struct {
|
||||
regexp *regexp.Regexp
|
||||
replacement string
|
||||
}
|
||||
|
||||
func (p pathRewrite) Wrap(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.RequestURI = p.regexp.ReplaceAllString(r.RequestURI, p.replacement)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user