You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
799 B
40 lines
799 B
package endpoint
|
|
|
|
import (
|
|
"net/url"
|
|
"strconv"
|
|
)
|
|
|
|
// NewEndpoint new an Endpoint URL.
|
|
func NewEndpoint(scheme, host string, isSecure bool) *url.URL {
|
|
var query string
|
|
if isSecure {
|
|
query = "isSecure=true"
|
|
}
|
|
return &url.URL{Scheme: scheme, Host: host, RawQuery: query}
|
|
}
|
|
|
|
// ParseEndpoint parses an Endpoint URL.
|
|
func ParseEndpoint(endpoints []string, scheme string, isSecure bool) (string, error) {
|
|
for _, e := range endpoints {
|
|
u, err := url.Parse(e)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if u.Scheme == scheme {
|
|
if IsSecure(u) == isSecure {
|
|
return u.Host, nil
|
|
}
|
|
}
|
|
}
|
|
return "", nil
|
|
}
|
|
|
|
// IsSecure parses isSecure for Endpoint URL.
|
|
func IsSecure(u *url.URL) bool {
|
|
ok, err := strconv.ParseBool(u.Query().Get("isSecure"))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return ok
|
|
}
|
|
|