-
Notifications
You must be signed in to change notification settings - Fork 65
/
labeled.go
93 lines (73 loc) · 1.95 KB
/
labeled.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
package config
import (
"net/http"
"net/url"
"golang.ngrok.com/ngrok/internal/tunnel/proto"
)
type LabeledTunnelOption interface {
ApplyLabeled(cfg *labeledOptions)
}
type labeledOptionFunc func(cfg *labeledOptions)
func (of labeledOptionFunc) ApplyLabeled(cfg *labeledOptions) {
of(cfg)
}
// LabeledTunnel constructs a new set options for a labeled Edge.
//
// https://ngrok.com/docs/network-edge/edges/#tunnel-group
func LabeledTunnel(opts ...LabeledTunnelOption) Tunnel {
cfg := labeledOptions{}
for _, opt := range opts {
opt.ApplyLabeled(&cfg)
}
return &cfg
}
// Options for labeled tunnels.
type labeledOptions struct {
// Common tunnel configuration options.
commonOpts
// A map of label, value pairs for this tunnel.
labels map[string]string
// An HTTP Server to run traffic on
// Deprecated: Pass HTTP server refs via session.ListenAndServeHTTP instead.
httpServer *http.Server
}
// WithLabel adds a label to this tunnel's set of label, value pairs.
func WithLabel(label, value string) LabeledTunnelOption {
return labeledOptionFunc(func(cfg *labeledOptions) {
if cfg.labels == nil {
cfg.labels = map[string]string{}
}
cfg.labels[label] = value
})
}
func (cfg labeledOptions) ForwardsProto() string {
return cfg.commonOpts.ForwardsProto
}
func (cfg labeledOptions) ForwardsTo() string {
return cfg.commonOpts.getForwardsTo()
}
func (cfg *labeledOptions) WithForwardsTo(url *url.URL) {
cfg.commonOpts.ForwardsTo = url.Host
}
func (cfg labeledOptions) Extra() proto.BindExtra {
return proto.BindExtra{
Metadata: cfg.Metadata,
}
}
func (cfg labeledOptions) Proto() string {
return ""
}
func (cfg labeledOptions) Opts() any {
return nil
}
func (cfg labeledOptions) Labels() map[string]string {
return cfg.labels
}
func (cfg labeledOptions) HTTPServer() *http.Server {
return cfg.httpServer
}
// compile-time check that we're implementing the proper interfaces.
var _ interface {
tunnelConfigPrivate
Tunnel
} = (*labeledOptions)(nil)