Ver Fonte

:gear: Construct from client the socket dialer

Ettore Di Giacinto há 3 anos atrás
pai
commit
c27cda6965
2 ficheiros alterados com 15 adições e 9 exclusões
  1. 1 9
      api/api_test.go
  2. 14 0
      api/client/client.go

+ 1 - 9
api/api_test.go

@@ -19,8 +19,6 @@ import (
 	"context"
 	"fmt"
 	"io/ioutil"
-	"net"
-	"net/http"
 	"os"
 	"path/filepath"
 	"time"
@@ -44,13 +42,7 @@ var _ = Describe("API", func() {
 			os.MkdirAll(d, os.ModePerm)
 			socket := filepath.Join(d, "socket")
 
-			c := client.NewClient(client.WithHost("http://unix"), client.WithHTTPClient(&http.Client{
-				Transport: &http.Transport{
-					DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
-						return net.Dial("unix", socket)
-					},
-				},
-			}))
+			c := client.NewClient(client.WithHost("unix://" + socket))
 
 			token := node.GenerateNewConnectionData().Base64()
 			ctx, cancel := context.WithCancel(context.Background())

+ 14 - 0
api/client/client.go

@@ -16,11 +16,14 @@
 package client
 
 import (
+	"context"
 	"encoding/base64"
 	"encoding/json"
 	"fmt"
 	"io/ioutil"
+	"net"
 	"net/http"
+	"strings"
 	"time"
 
 	"github.com/mudler/edgevpn/pkg/blockchain"
@@ -47,6 +50,17 @@ const (
 func WithHost(host string) func(c *Client) error {
 	return func(c *Client) error {
 		c.host = host
+		if strings.HasPrefix(host, "unix://") {
+			socket := strings.ReplaceAll(host, "unix://", "")
+			c.host = "http://unix"
+			c.httpClient = &http.Client{
+				Transport: &http.Transport{
+					DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
+						return net.Dial("unix", socket)
+					},
+				},
+			}
+		}
 		return nil
 	}
 }