From 98eab14e7a49d2312fa76e5a189acac9e0547b7f Mon Sep 17 00:00:00 2001 From: Chetan Sarva Date: Wed, 3 Nov 2021 17:09:24 -0400 Subject: [PATCH] feat: cleanly replace an existing vhost --- .gitignore | 2 +- daemon.go | 21 ++++++++++----------- vhost.go | 17 ++++++++++++++++- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 29f0a63..ac9f88a 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,4 @@ _testmain.go /vproxy* dist/ .vproxy.conf - +t/ diff --git a/daemon.go b/daemon.go index bb4c38d..3063f4c 100644 --- a/daemon.go +++ b/daemon.go @@ -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 @@ -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. diff --git a/vhost.go b/vhost.go index 2daff9b..53c7589 100644 --- a/vhost.go +++ b/vhost.go @@ -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() {