Skip to content

Commit

Permalink
feat: cleanly replace an existing vhost
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan committed Nov 3, 2021
1 parent 574e587 commit 98eab14
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ _testmain.go
/vproxy*
dist/
.vproxy.conf

t/
21 changes: 10 additions & 11 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,9 @@ func (d *Daemon) relayLogsUntilClose(vhost *Vhost, w http.ResponseWriter, reqCtx
logChan := vhost.NewLogListener()

// read existing logs first
if vhost.logRing.Len() > 0 {
buff := ""
for i := 0; i < vhost.logRing.Len(); i++ {
s := vhost.logRing.At(i).(string)
if s != "" {
buff += s + "\n"
}
}
if buff != "" {
fmt.Fprint(w, buff)
}
buff := vhost.BufferAsString()
if buff != "" {
fmt.Fprint(w, buff)
}

// Listen to connection close and un-register logChan
Expand All @@ -254,6 +246,13 @@ func (d *Daemon) addVhost(binding string, w http.ResponseWriter) *Vhost {
return nil
}

// remove any existing vhost
if v := d.loggedHandler.GetVhost(vhost.Host); v != nil {
fmt.Printf("[*] removing existing vhost: %s -> %d\n", v.Host, v.Port)
v.Close()
d.loggedHandler.RemoveVhost(vhost.Host)
}

fmt.Printf("[*] registering new vhost: %s -> %d\n", vhost.Host, vhost.Port)

// Set the headers related to event streaming.
Expand Down
17 changes: 16 additions & 1 deletion vhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,23 @@ func (v *Vhost) RemoveLogListener(logChan LogListener) {
v.listeners = v.listeners[:index]
}

func (v *Vhost) Close() {
func (v *Vhost) BufferAsString() string {
if v.logRing.Len() == 0 {
return ""
}
buff := ""
for i := 0; i < v.logRing.Len(); i++ {
s := v.logRing.At(i).(string)
if s != "" {
buff += s + "\n"
}
}
return buff
}

func (v *Vhost) Close() {
close(v.logChan)
v.logRing.Clear()
}

func (v *Vhost) populateLogBuffer() {
Expand Down

0 comments on commit 98eab14

Please sign in to comment.