-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
57 lines (49 loc) · 1.79 KB
/
application.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
51
52
53
54
55
56
57
package scrimplb
import (
"sort"
"strings"
)
// JSONApplication is a helper for loading applications with string slices for Domains
type JSONApplication struct {
Name string `json:"name"`
ListenPort string `json:"listen-port"`
ApplicationPort string `json:"application-port"`
Protocol string `json:"protocol"`
Domains []string `json:"domains"`
}
// ToApplication turns a JSON loaded application into an application. This is needed to keep
// Applications comparable (since slices aren't) and therefore usable as map keys.
func (a *JSONApplication) ToApplication() Application {
sort.Slice(a.Domains, func(i, j int) bool {
return a.Domains[i] < a.Domains[j]
})
domainString := strings.Join(a.Domains, " ")
return Application{
Name: a.Name,
ListenPort: a.ListenPort,
ApplicationPort: a.ApplicationPort,
Protocol: a.Protocol,
domains: domainString,
}
}
// Application is a service running on a backend. A backend will respond
// with a list of Applications when queried by a load balancer.
type Application struct {
Name string
ListenPort string
ApplicationPort string
Protocol string
domains string
}
// Equal implements an equality check for two Applications
func (a *Application) Equal(other Application) bool {
return a.Name == other.Name && a.ListenPort == other.ListenPort && a.ApplicationPort == other.ApplicationPort && a.Protocol == other.Protocol && a.domains == other.domains
}
// DomainSlice returns domains as a []string
func (a *Application) DomainSlice() []string {
return strings.Split(a.domains, " ")
}
// DomainString returns the domain list as a string separated by `sep`
func (a *Application) DomainString(sep string) string {
return strings.Join(a.DomainSlice(), sep)
}