api/internal/router/helper.go
2025-07-07 17:46:31 +03:00

64 lines
1.4 KiB
Go

package router
import (
"fmt"
"merch-parser-api/internal/shared"
"net/http"
"strings"
)
func excluded(prefix, path, method string, excludedRoutes *map[string]shared.ExcludeRoute) bool {
if excludedRoutes == nil {
return false
}
nPath := normalizePath(path)
switch {
case nPath == "/"+prefix && method == http.MethodGet: //ignore api base path for GET method
return true
case nPath == "/swagger/*any":
return true
case nPath == "/version":
return true
}
if value, ok := (*excludedRoutes)[nPath]; ok {
fmt.Println(value, ok)
if isSameRoute(nPath, value.Route) && value.Method == method {
return true
}
}
return false
}
func normalizePath(path string) string {
return strings.TrimRight(path, "/")
}
func joinPaths(paths ...string) string {
result := ""
for _, p := range paths {
if result == "" {
result = strings.TrimRight(p, "/")
} else {
result = result + "/" + strings.Trim(p, "/")
}
}
return "/" + strings.TrimLeft(result, "/")
}
func isSameRoute(path, excluded string) bool {
path = strings.Split(path, "?")[0]
path = strings.TrimSuffix(path, "/")
excluded = strings.TrimSuffix(excluded, "/")
if strings.HasSuffix(excluded, "/*any") || strings.HasSuffix(excluded, "/*") {
base := strings.TrimSuffix(excluded, "/*any")
base = strings.TrimSuffix(base, "/*")
return strings.HasPrefix(path, base)
}
return path == excluded
}