mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
minor improvement of the context.HandlerName
This commit is contained in:
parent
66e3c26efe
commit
ddda4f6998
|
@ -65,6 +65,7 @@ func SetHandlerName(original string, replacement string) {
|
||||||
regex, _ := regexp.Compile(original)
|
regex, _ := regexp.Compile(original)
|
||||||
handlerNames[&NameExpr{
|
handlerNames[&NameExpr{
|
||||||
literal: original,
|
literal: original,
|
||||||
|
normalizedLiteral: normalizeExpression(original),
|
||||||
regex: regex,
|
regex: regex,
|
||||||
}] = replacement
|
}] = replacement
|
||||||
|
|
||||||
|
@ -75,6 +76,7 @@ func SetHandlerName(original string, replacement string) {
|
||||||
type NameExpr struct {
|
type NameExpr struct {
|
||||||
regex *regexp.Regexp
|
regex *regexp.Regexp
|
||||||
literal string
|
literal string
|
||||||
|
normalizedLiteral string
|
||||||
}
|
}
|
||||||
|
|
||||||
// MatchString reports whether "s" is literal of "literal"
|
// MatchString reports whether "s" is literal of "literal"
|
||||||
|
@ -91,6 +93,23 @@ func (expr *NameExpr) MatchString(s string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MatchFilename reports whether "filename" contains the "literal".
|
||||||
|
func (expr *NameExpr) MatchFilename(filename string) bool {
|
||||||
|
if filename == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Contains(filename, expr.normalizedLiteral)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The regular expression to match the versioning and the domain part
|
||||||
|
var trimFileModuleNameRegex = regexp.MustCompile(`^[\w.]+/(kataras|iris-contrib)/|/v\d+|\.\*`)
|
||||||
|
|
||||||
|
func normalizeExpression(str string) string {
|
||||||
|
// Replace all occurrences of the regular expression with the replacement string.
|
||||||
|
return strings.ToLower(trimFileModuleNameRegex.ReplaceAllString(str, ""))
|
||||||
|
}
|
||||||
|
|
||||||
// A Handler responds to an HTTP request.
|
// A Handler responds to an HTTP request.
|
||||||
// It writes reply headers and data to the Context.ResponseWriter() and then return.
|
// It writes reply headers and data to the Context.ResponseWriter() and then return.
|
||||||
// Returning signals that the request is finished;
|
// Returning signals that the request is finished;
|
||||||
|
@ -125,11 +144,14 @@ func valueOf(v interface{}) reflect.Value {
|
||||||
// See `SetHandlerName` too.
|
// See `SetHandlerName` too.
|
||||||
func HandlerName(h interface{}) string {
|
func HandlerName(h interface{}) string {
|
||||||
pc := valueOf(h).Pointer()
|
pc := valueOf(h).Pointer()
|
||||||
name := runtime.FuncForPC(pc).Name()
|
fn := runtime.FuncForPC(pc)
|
||||||
|
name := fn.Name()
|
||||||
|
filename, _ := fn.FileLine(fn.Entry())
|
||||||
|
filenameLower := strings.ToLower(filename)
|
||||||
|
|
||||||
handlerNamesMu.RLock()
|
handlerNamesMu.RLock()
|
||||||
for expr, newName := range handlerNames {
|
for expr, newName := range handlerNames {
|
||||||
if expr.MatchString(name) {
|
if expr.MatchString(name) || expr.MatchFilename(filenameLower) {
|
||||||
name = newName
|
name = newName
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user