Skip to content

Commit

Permalink
perf(middleware/nethttp): Optimize DefaultTenantFromSubdomain Function
Browse files Browse the repository at this point in the history
Optimizes the `DefaultTenantFromSubdomain` function within the `nethttp` middleware to improve performance and readability.
  • Loading branch information
bartventer committed Jul 23, 2024
1 parent 1867ff8 commit 7e6439a
Showing 1 changed file with 2 additions and 26 deletions.
28 changes: 2 additions & 26 deletions middleware/nethttp/nethttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)

Expand All @@ -51,32 +50,9 @@ func DefaultSkipper(r *http.Request) bool {
return false
}

// DefaultTenantFromSubdomain extracts the subdomain from the given HTTP request's host.
// It removes the port from the host if present and adds a scheme to the host for parsing.
// The function then parses the URL and extracts the subdomain.
// It returns the extracted subdomain as a string and any error encountered during the process.
//
// This function calls the [ExtractSubdomain] function to extract the subdomain from the host.
// DefaultTenantFromSubdomain extracts the tenant from the subdomain in the HTTP request.
func DefaultTenantFromSubdomain(r *http.Request) (string, error) {
// Extract the host from the request
host := r.Host

// If the host includes a port, remove it
if strings.Contains(host, ":") {
host = strings.Split(host, ":")[0]
}

// Add a scheme to the host so it can be parsed by url.Parse
urlStr := fmt.Sprintf("https://%s", host)

// Parse the URL
u, err := url.Parse(urlStr)
if err != nil {
return "", err
}

// Extract the subdomain
return ExtractSubdomain(u.String())
return ExtractSubdomain(r.Host)
}

// DefaultTenantFromHeader extracts the tenant from the [XTenantHeader] header in the HTTP request.
Expand Down

0 comments on commit 7e6439a

Please sign in to comment.