Skip to content

Commit

Permalink
Optimize computeRoutePrefix
Browse files Browse the repository at this point in the history
Previously, computeRoutePrefix was adding a '/' to the start of the prefix and then removing it from the end, regardless of whether it was there or not. This resulted in unnecessary string operations. The updated function only adds or removes the '/' when necessary, improving efficiency. Additionally, the function now uses strings.HasPrefix and strings.HasSuffix for clearer intent and readability.

Signed-off-by: donuts-are-good <[email protected]>
  • Loading branch information
donuts-are-good authored Oct 7, 2023
1 parent 9eb0adc commit 86430ae
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,23 @@ func computeRoutePrefix(prefix string, externalURL *url.URL) string {
}

if prefix == "/" {
prefix = ""
return ""
}

if prefix != "" {
prefix = "/" + strings.Trim(prefix, "/")
// Ensure prefix starts with "/"
if !strings.HasPrefix(prefix, "/") {
prefix = "/" + prefix
}

// Ensure prefix does not end with "/"
if strings.HasSuffix(prefix, "/") {
prefix = strings.TrimSuffix(prefix, "/")
}

return prefix
}


// shutdownServerOnQuit shutdowns the provided server upon closing the provided
// quitCh or upon receiving a SIGINT or SIGTERM.
func shutdownServerOnQuit(server *http.Server, quitCh <-chan struct{}, logger log.Logger) error {
Expand Down

0 comments on commit 86430ae

Please sign in to comment.