Cleaner initialization

This commit is contained in:
Ian Gulliver
2023-06-07 23:24:32 -07:00
parent 4d806f5d59
commit 7adc14d656
3 changed files with 32 additions and 5 deletions

View File

@@ -2,9 +2,12 @@ package proxy
import (
"errors"
"fmt"
"net"
"sync"
"testing"
"github.com/stretchr/testify/require"
)
type Proxy struct {
@@ -17,7 +20,7 @@ type Proxy struct {
mu sync.Mutex
}
func NewProxy(t *testing.T, backend *net.TCPAddr) (*Proxy, error) {
func NewProxy(t *testing.T, backend *net.TCPAddr) *Proxy {
var err error
p := &Proxy{
@@ -27,21 +30,27 @@ func NewProxy(t *testing.T, backend *net.TCPAddr) (*Proxy, error) {
}
p.listener, err = net.ListenTCP("tcp", nil)
if err != nil {
return nil, err
}
require.NoError(t, err)
go p.accept()
t.Logf("* -> %s -> [proxy] -> * -> %s listening...", p.listener.Addr(), p.backend)
return p, nil
return p
}
func (p *Proxy) Addr() *net.TCPAddr {
return p.listener.Addr().(*net.TCPAddr)
}
func (p *Proxy) HTTP() string {
return fmt.Sprintf("http://%s/", p.Addr())
}
func (p *Proxy) HTTPS() string {
return fmt.Sprintf("https://%s/", p.Addr())
}
func (p *Proxy) CloseAllConns() {
p.mu.Lock()
defer p.mu.Unlock()