Skip to content

Commit

Permalink
apiserver: fixing the trim right path;
Browse files Browse the repository at this point in the history
  • Loading branch information
Milad Poshtdari authored and selm0 committed Mar 17, 2023
1 parent 5f63ab3 commit f2c103d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
14 changes: 12 additions & 2 deletions pkg/apiserver/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (d *Definition) getAbsolutePath() string {
groupPath := d.group.getAbsolutePath()

absolutePath := fmt.Sprintf("%s/%s", groupPath, d.relativePath)
absolutePath = strings.TrimRight(absolutePath, "/")
absolutePath = trimRightPath(absolutePath)

return removeDuplicates(absolutePath)
}
Expand Down Expand Up @@ -67,7 +67,7 @@ func (d *Definitions) Use(middleware ...gin.HandlerFunc) {
}

func (d *Definitions) Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) {
relativePath = strings.TrimRight(relativePath, "/")
relativePath = trimRightPath(relativePath)

d.routes = append(d.routes, Definition{
group: d,
Expand Down Expand Up @@ -139,3 +139,13 @@ func removeDuplicates(s string) string {

return buf.String()
}

func trimRightPath(path string) string {
absolutePath := strings.TrimRight(path, "/")

if absolutePath == "" {
absolutePath = "/"
}

return absolutePath
}
7 changes: 1 addition & 6 deletions pkg/apiserver/middleware_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package apiserver

import (
"fmt"
"strings"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -35,12 +34,8 @@ func metricMiddleware(ginCtx *gin.Context, writer metric.Writer) {
return
}

path = strings.TrimRight(path, "/")
path = trimRightPath(path)
path = removeDuplicates(path)
if path == "" {
// we have a route at the root, so we have to use a single slash; empty dimensions are forbidden by cloudwatch
path = "/"
}

ginCtx.Next()

Expand Down

0 comments on commit f2c103d

Please sign in to comment.