@@ -15,18 +15,18 @@ import (
15
15
16
16
// Vhost represents a single backend service
17
17
type Vhost struct {
18
- Host string // virtual host name
18
+ Host string `json:"host"` // virtual host name
19
19
20
20
ServiceHost string // service host or IP
21
21
ServicePort int // service port
22
22
23
- Handler http.Handler
24
- Cert string // TLS Certificate
25
- Key string // TLS Private Key
23
+ Handler http.Handler `json:"-"`
24
+ Cert string // TLS Certificate
25
+ Key string // TLS Private Key
26
26
27
- logRing * deque.Deque [string ]
28
- logChan LogListener
29
- listeners []LogListener
27
+ logRing * deque.Deque [string ] `json:"-"`
28
+ logChan LogListener `json:"-"`
29
+ listeners []LogListener `json:"-"`
30
30
}
31
31
32
32
type LogListener chan string
@@ -107,22 +107,13 @@ func CreateVhost(input string, useTLS bool) (*Vhost, error) {
107
107
return nil , fmt .Errorf ("failed to parse target port: %s" , err )
108
108
}
109
109
targetHost := "127.0.0.1"
110
- targetURL := url.URL {Scheme : "http" , Host : fmt .Sprintf ("%s:%d" , targetHost , targetPort )}
111
-
112
- proxy := CreateProxy (targetURL , hostname )
113
110
114
111
vhost := & Vhost {
115
- Host : hostname , ServiceHost : targetHost , ServicePort : targetPort , Handler : proxy ,
116
- logRing : & deque. Deque [ string ]{} ,
117
- logChan : make ( LogListener , 10 ) ,
112
+ Host : hostname ,
113
+ ServiceHost : targetHost ,
114
+ ServicePort : targetPort ,
118
115
}
119
116
120
- // set fixed capacity at 16
121
- vhost .logRing .Grow (16 )
122
- vhost .logRing .SetBaseCap (16 )
123
-
124
- go vhost .populateLogBuffer ()
125
-
126
117
if useTLS {
127
118
vhost .Cert , vhost .Key , err = MakeCert (hostname )
128
119
if err != nil {
@@ -133,6 +124,17 @@ func CreateVhost(input string, useTLS bool) (*Vhost, error) {
133
124
return vhost , nil
134
125
}
135
126
127
+ func (v * Vhost ) Init () {
128
+ targetURL := url.URL {Scheme : "http" , Host : fmt .Sprintf ("%s:%d" , v .ServiceHost , v .ServicePort )}
129
+ v .Handler = CreateProxy (targetURL , v .Host )
130
+ v .logChan = make (LogListener , 10 )
131
+ // set fixed capacity at 16
132
+ v .logRing = & deque.Deque [string ]{}
133
+ v .logRing .Grow (16 )
134
+ v .logRing .SetBaseCap (16 )
135
+ go v .populateLogBuffer ()
136
+ }
137
+
136
138
func (v * Vhost ) NewLogListener () LogListener {
137
139
logChan := make (LogListener , 100 )
138
140
v .listeners = append (v .listeners , logChan )
@@ -180,6 +182,10 @@ func (v *Vhost) populateLogBuffer() {
180
182
}
181
183
}
182
184
185
+ func (v Vhost ) String () string {
186
+ return fmt .Sprintf ("%s -> %s:%d" , v .Host , v .ServiceHost , v .ServicePort )
187
+ }
188
+
183
189
// Map given host to 127.0.0.1 in system hosts file (usually /etc/hosts)
184
190
func addToHosts (host string ) error {
185
191
hosts , err := txeh .NewHostsDefault ()
0 commit comments