/
address.go
164 lines (125 loc) · 3.13 KB
/
address.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package webfmwk
import (
"fmt"
"log/slog"
"strconv"
"strings"
"github.com/burgesQ/webfmwk/v6/tls"
)
const _unixSocketPrefix = `unix://`
type (
// IAddress interface hold an api server listing configuration
IAddress interface {
fmt.Stringer
// GetAddr return the listing address.
GetAddr() string
// GetTLS return a pointer to an TLSConfig if present, nil otherwise.
GetTLS() tls.IConfig
// GetName return the name of the address, for debug purpose.
GetName() string
// IsOk validate that the Address structure have at least the address field populated.
IsOk() bool
// IsUnixPath return true if the address is a valid unix socket path.
IsUnixPath() bool
// SameAs return true if both config are identique.
SameAs(in IAddress) bool
}
// Address implement the IAddress interface
Address struct {
// TLS implement IAddress, tlsConfig implement the TLSConfig interface.
TLS *tls.Config `json:"tls,omitempty" mapstructure:"tls,omitempty"`
Addr string `json:"addr"`
Name string `json:"name"`
}
Addresses []Address
)
func (a Address) SameAs(in IAddress) bool {
var (
tlsOk = false
itls = in.GetTLS()
)
if a.TLS == nil && itls == nil {
tlsOk = true
} else if a.TLS != nil && itls != nil {
tlsOk = a.TLS.SameAs(itls)
}
return a.Addr == in.GetAddr() && a.Name == in.GetName() && tlsOk
}
// SameAs return true if all addresses in the in param match
// One from the struct.
func (a Addresses) SameAs(in Addresses) bool {
if len(a) != len(in) { // not same nb of address
return false
}
for i := range a {
iok := false
for j := range in {
if in[j].SameAs(a[i]) {
iok = true
break
}
}
if !iok {
return false
}
}
return true
}
func (a Addresses) String() (ret string) {
ret = "\t --- number of address(es): " + strconv.Itoa(len(a))
for i := range a {
ret += "\n" + a[i].String()
}
ret += "\n\t --- end address"
return
}
func (a Addresses) AsAttrs() []any {
r := make([]any, len(a))
for i := range a {
r[i] = slog.Group(fmt.Sprintf("address %d", i), a[i].AsAttrs()...)
}
return r
}
func Tern[T any](cond bool, t, f func() T) T {
if cond {
return t()
}
return f()
}
func (a Address) AsAttrs() []any {
return []any{
slog.String("name", a.Name),
slog.String("address", a.Addr),
slog.Any("tls", a.TLS),
}
}
// String implement the fmt.Stringer interface
func (a Address) String() string {
if a.TLS != nil && !a.TLS.Empty() {
return fmt.Sprintf("\n\t -!- name: %q\n\t -!- addr: %q\n\t -!- tls:\n%s", a.Name, a.Addr, a.TLS.String())
}
return fmt.Sprintf("name: %q\naddr: %q", a.Name, a.Addr)
}
// IsOk implement the IAddress interface
func (a Address) IsUnixPath() bool {
return strings.HasPrefix(a.Addr, _unixSocketPrefix)
}
// IsOk implement the IAddress interface
func (a Address) IsOk() bool {
return a.Addr != ""
}
// GetAddr implement the IAddress interface
func (a Address) GetAddr() string {
return a.Addr
}
// GetTLS implement the IAddress interface
func (a Address) GetTLS() tls.IConfig {
if a.TLS == nil {
return nil
}
return *a.TLS
}
// GetName implement the IAddress interface
func (a Address) GetName() string {
return a.Name
}