added: auth middleware
This commit is contained in:
parent
099a586484
commit
7e0c1e71de
3 changed files with 147 additions and 3 deletions
64
internal/router/helper.go
Normal file
64
internal/router/helper.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue