-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
42 lines (37 loc) · 802 Bytes
/
config.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
package load_balancer
import (
"fmt"
"net/url"
"os"
)
type Configuration struct {
backends *[]url.URL
}
func Config() Configuration {
return Configuration{
backends: GetBackends(),
}
}
// Returns a list of available backend API endpoints.
func GetBackends() *[]url.URL {
var backends []url.URL
for i := 1; i > 0; i++ {
backend := os.Getenv(fmt.Sprintf("BACKEND_%v", i))
url, err := url.Parse(backend)
if backend == "" || err != nil {
break
}
fmt.Println(fmt.Sprintf("Configuring backend %q", backend))
backends = append(backends, *url)
}
return &backends
}
// Checks for an existing environment variable. If none exists,
// return a default.
func Getenv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}