zrok/endpoints/util.go

55 lines
1.3 KiB
Go
Raw Permalink Normal View History

package endpoints
import (
2023-05-25 17:50:38 +02:00
"github.com/openziti/edge-api/rest_model"
"github.com/openziti/sdk-golang/ziti"
"github.com/sirupsen/logrus"
2023-05-25 20:59:39 +02:00
"net/url"
"strings"
)
2023-05-25 17:50:38 +02:00
func GetRefreshedService(name string, ctx ziti.Context) (*rest_model.ServiceDetail, bool) {
svc, found := ctx.GetService(name)
if !found {
if err := ctx.RefreshServices(); err != nil {
logrus.Errorf("error refreshing services: %v", err)
return nil, false
}
return ctx.GetService(name)
}
return svc, found
}
func JoinURLPath(a, b *url.URL) (path, rawpath string) {
if a.RawPath == "" && b.RawPath == "" {
return singleJoiningSlash(a.Path, b.Path), ""
}
// Same as singleJoiningSlash, but uses EscapedPath to determine
// whether a slash should be added
apath := a.EscapedPath()
bpath := b.EscapedPath()
aslash := strings.HasSuffix(apath, "/")
bslash := strings.HasPrefix(bpath, "/")
switch {
case aslash && bslash:
return a.Path + b.Path[1:], apath + bpath[1:]
case !aslash && !bslash:
return a.Path + "/" + b.Path, apath + "/" + bpath
}
return a.Path + b.Path, apath + bpath
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}