package util /* Copyright 2024 Jonas Kaninda. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may get a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 */ import ( "net/url" "os" "strconv" "strings" ) // FileExists checks if the file does exist func FileExists(filename string) bool { info, err := os.Stat(filename) if os.IsNotExist(err) { return false } return !info.IsDir() } func GetStringEnv(key, defaultValue string) string { val := os.Getenv(key) if val == "" { return defaultValue } return val } func GetIntEnv(key string, defaultValue int) int { val := os.Getenv(key) if val == "" { return defaultValue } i, err := strconv.Atoi(val) if err != nil { return defaultValue } return i } func GetBoolEnv(key string, defaultValue bool) bool { val := os.Getenv(key) if val == "" { return defaultValue } b, err := strconv.ParseBool(val) if err != nil { return defaultValue } return b } // SetEnv Set env func SetEnv(name, value string) { err := os.Setenv(name, value) if err != nil { return } } func MergeSlices(slice1, slice2 []string) []string { return append(slice1, slice2...) } // ParseURLPath returns a URL path func ParseURLPath(urlPath string) string { // Replace any double slashes with a single slash urlPath = strings.ReplaceAll(urlPath, "//", "/") // Ensure the path starts with a single leading slash if !strings.HasPrefix(urlPath, "/") { urlPath = "/" + urlPath } return urlPath } func ParseRoutePath(path, blockedPath string) string { basePath := ParseURLPath(path) switch { case blockedPath == "": return basePath case strings.HasSuffix(blockedPath, "/*"): return basePath + blockedPath[:len(blockedPath)-2] case strings.HasSuffix(blockedPath, "*"): return basePath + blockedPath[:len(blockedPath)-1] default: return basePath + blockedPath } } func UrlParsePath(uri string) string { parse, err := url.Parse(uri) if err != nil { return "" } return parse.Path }