-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservers.go
242 lines (189 loc) · 4.74 KB
/
servers.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package servers
import (
"fmt"
"io"
"net/http"
"github.com/go-x-pkg/dumpctx"
"google.golang.org/grpc"
)
type (
iterCb func(Server) bool
iterator func(iterCb) bool
)
func (it iterator) IntoIter() iterator { return it }
func (it iterator) Len() (length int) {
it(func(Server) bool {
length++
return true
})
return
}
func (it iterator) First() (srv Server) {
it(func(s Server) bool {
srv = s
return false
})
return srv
}
func (it iterator) Filter(take func(Server) bool) iterator {
return iterator(func(cb iterCb) bool {
return it(func(s Server) bool {
if take(s) {
if !cb(s) {
return false
}
}
return true
})
})
}
func takeUnix(s Server) bool { return s.Kind().Has(KindUNIX) }
func takeInet(s Server) bool { return s.Kind().Has(KindINET) }
func takeHTTP(s Server) bool { return s.Kind().Has(KindHTTP) }
func takeGRPC(s Server) bool { return s.Kind().Has(KindGRPC) }
func (it iterator) FilterUnix() iterator { return it.Filter(takeUnix) }
func (it iterator) FilterInet() iterator { return it.Filter(takeInet) }
func (it iterator) FilterHTTP() iterator { return it.Filter(takeHTTP) }
func (it iterator) FilterGRPC() iterator { return it.Filter(takeGRPC) }
func (it iterator) FilterListener() iterator {
return it.Filter(func(s Server) bool {
_, ok := s.(*ServerListener)
return ok
})
}
func (it iterator) Defaultize(inetHost string, inetPort int, unixAddr string) (err error) {
it(func(s Server) bool {
if defaultizer, ok := s.(serverDefaultizer); ok {
if e := defaultizer.defaultize(); e != nil {
err = e
return false
}
}
return true
})
if err != nil {
return err
}
if s := it.FilterUnix().First(); s != nil {
unix := s.(*ServerUNIX)
if unix.Addr() == "" {
unix.Address = unixAddr
}
}
if s := it.FilterInet().First(); s != nil {
inet := s.(*ServerINET)
if inet.Host == "" {
inet.Host = inetHost
}
if inet.Port == 0 {
inet.Port = inetPort
}
}
return err
}
func (it iterator) Interpolate(interpolateFn func(string) string) {
it(func(s Server) bool {
if interpolator, ok := s.(serverInterpolator); ok {
interpolator.interpolate(interpolateFn)
}
return true
})
}
func (it iterator) Validate() (err error) {
it(func(s Server) bool {
if validator, ok := s.(serverValidator); ok {
if e := validator.validate(); e != nil {
err = e
return false
}
}
return true
})
return err
}
func (it iterator) Dump(ctx *dumpctx.Ctx, w io.Writer) {
if ln := it.Len(); ln != 0 {
fmt.Fprintf(w, " (x%d):\n", ln)
it(func(s Server) bool {
ctx.WrapList(func() {
ctx.EmitPrefix(w)
fmt.Fprintf(w, "kind: %s\n", s.Kind())
ctx.Enter()
defer ctx.Leave()
fmt.Fprintf(w, "%snetwork: %s\n", ctx.Indent(), s.Network())
if dumper, ok := s.(serverDumper); ok {
dumper.Dump(ctx, w)
}
})
return true
})
} else {
fmt.Fprint(w, ": ~\n")
}
}
type Servers []*ServerWrapped
func (ss Servers) IntoIter() iterator { return ss.ForEach }
func (ss Servers) ForEach(cb iterCb) bool {
for _, s := range ss {
if !cb(s.Server) {
return false
}
}
return true
}
func (ss Servers) Defaultize(inetHost string, inetPort int, unixAddr string) (err error) {
return ss.IntoIter().Defaultize(inetHost, inetPort, unixAddr)
}
func (ss Servers) Dump(ctx *dumpctx.Ctx, w io.Writer) { ss.IntoIter().Dump(ctx, w) }
func (ss Servers) Validate() error { return ss.IntoIter().Validate() }
func (ss Servers) Interpolate(interpolateFn func(string) string) {
ss.IntoIter().Interpolate(interpolateFn)
}
func (ss *Servers) PushINETIfNotExists(host string, port int, kind Kind) {
if ss.IntoIter().FilterInet().Len() != 0 {
return
}
var server ServerINET
server.Knd = KindINET
server.Knd.Set(kind)
server.Host = host
server.Port = port
*ss = append(*ss, serverEnsureWrapped(&server))
}
func (ss *Servers) PushUnixIfNotExists(addr string, kind Kind) {
if ss.IntoIter().FilterUnix().Len() != 0 {
return
}
var server ServerUNIX
server.Knd = KindUNIX
server.Knd.Set(kind)
server.Address = addr
*ss = append(*ss, serverEnsureWrapped(&server))
}
func (ss *Servers) SetPortToFirstINET(port int) {
ss.
IntoIter().
FilterInet().
First().(*ServerINET).
setPort(port)
}
func (ss *Servers) Listen(fnArgs ...Arg) (Servers, []error) {
return ss.IntoIter().Listen(fnArgs...)
}
func (ss *Servers) ServeHTTP(fnNewServer func(Server) http.Handler, fnArgs ...Arg) error {
return ss.
IntoIter().
FilterHTTP().
FilterListener().
ServeHTTP(fnNewServer, fnArgs...)
}
func (ss *Servers) ServeGRPC(
fnNewServer func(s Server, opts ...grpc.ServerOption) *grpc.Server, fnArgs ...Arg,
) error {
return ss.
IntoIter().
FilterGRPC().
FilterListener().
ServeGRPC(fnNewServer, fnArgs...)
}
func (ss *Servers) Close() []error { return ss.IntoIter().Close() }