/
setup.go
50 lines (47 loc) · 1.02 KB
/
setup.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
45
46
47
48
49
50
package slashquery
import (
"fmt"
"net/url"
"strings"
)
// Setup configures the upstream
func (sq *Slashquery) Setup() error {
for _, route := range sq.Routes {
if route.URL != "" {
u, err := url.Parse(route.URL)
if err != nil {
return err
}
route.Scheme = u.Scheme
route.Host = u.Host
route.Path = u.Path
route.rawQuery = u.RawQuery
if route.Upstream == "" {
// set only host without port
h := strings.Split(u.Host, ":")[0]
route.Upstream = h
if _, ok := sq.Upstreams[h]; !ok {
sq.Upstreams[h] = &Upstream{
Servers: []string{h},
}
}
}
continue
}
// set http or https
if route.Scheme == "" {
route.Scheme = "http"
} else if route.Scheme != "https" {
return fmt.Errorf("Unsuported scheme: %q", route.Scheme)
}
u, err := url.Parse(fmt.Sprintf("%s://%s%s", route.Scheme, route.Host, route.Path))
if err != nil {
return err
}
route.Scheme = u.Scheme
route.Host = u.Host
route.Path = u.Path
route.rawQuery = u.RawQuery
}
return nil
}