/
url.go
44 lines (38 loc) · 1.06 KB
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* *******************************************************************************
* * Copyright (c) 2020 Edgeworx, Inc.
* *
* * This program and the accompanying materials are made available under the
* * terms of the Eclipse Public License v. 2.0 which is available at
* * http://www.eclipse.org/legal/epl-2.0
* *
* * SPDX-License-Identifier: EPL-2.0
* *******************************************************************************
*
*/
package util
import (
"fmt"
"net/url"
)
func GetBaseURL(controllerEndpoint string) (*url.URL, error) {
u, err := url.Parse(controllerEndpoint)
if err != nil || u.Host == "" {
// Try to see if controllerEndpoint is an IP, in which case it needs to be pefixed by //
u, err = url.Parse("//" + controllerEndpoint)
if err != nil {
return nil, fmt.Errorf("failed to parse Controller URL (%s): %s", controllerEndpoint, err.Error())
}
}
// Default protocol
if u.Scheme == "" {
u.Scheme = "http"
}
// Default path
if u.Path == "" {
u.Path = "api/v3"
}
u.RawQuery = ""
u.Fragment = ""
return u, nil
}