Skip to content

Commit

Permalink
Remove nat package dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
antas-marcin committed Mar 27, 2024
1 parent c3af31b commit e2dd72b
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions modules/weaviate/weaviate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"fmt"
"time"

"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

const (
httpPort = nat.Port("8080/tcp")
grpcPort = nat.Port("50051/tcp")
image = "semitechnologies/weaviate:1.24.6"
httpPort = "8080/tcp"
grpcPort = "50051/tcp"
)

// WeaviateContainer represents the Weaviate container type used in the module
Expand All @@ -23,9 +23,9 @@ type WeaviateContainer struct {
// RunContainer creates an instance of the Weaviate container type
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*WeaviateContainer, error) {
req := testcontainers.ContainerRequest{
Image: "semitechnologies/weaviate:1.24.5",
Cmd: []string{"--host", "0.0.0.0", "--scheme", "http", "--port", httpPort.Port()},
ExposedPorts: []string{string(httpPort), string(grpcPort)},
Image: image,
Cmd: []string{"--host", "0.0.0.0", "--scheme", "http", "--port", "8080"},
ExposedPorts: []string{httpPort, grpcPort},
Env: map[string]string{
"AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED": "true",
"PERSISTENCE_DATA_PATH": "/var/lib/weaviate",
Expand Down Expand Up @@ -53,36 +53,34 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
return &WeaviateContainer{Container: container}, nil
}

func (c *WeaviateContainer) getHostAddress(ctx context.Context, port nat.Port) (string, error) {
containerPort, err := c.MappedPort(ctx, port)
// HttpHostAddress returns the schema and host of the Weaviate container.
// At the moment, it only supports the http scheme.
func (c *WeaviateContainer) HttpHostAddress(ctx context.Context) (string, string, error) {
port, err := c.MappedPort(ctx, httpPort)
if err != nil {
return "", fmt.Errorf("failed to get container port: %w", err)
return "", "", fmt.Errorf("failed to get container port: %w", err)
}

host, err := c.Host(ctx)
if err != nil {
return "", fmt.Errorf("failed to get container host")
return "", "", fmt.Errorf("failed to get container host")
}

return fmt.Sprintf("%s:%s", host, containerPort.Port()), nil
}

// HttpHostAddress returns the schema and host of the Weaviate container.
// At the moment, it only supports the http scheme.
func (c *WeaviateContainer) HttpHostAddress(ctx context.Context) (string, string, error) {
httpHostAddress, err := c.getHostAddress(ctx, httpPort)
if err != nil {
return "", "", err
}
return "http", httpHostAddress, nil
return "http", fmt.Sprintf("%s:%s", host, port.Port()), nil
}

// GrpcHostAddress returns the gRPC host of the Weaviate container.
// At the moment, it only supports unsecured gRPC connection.
func (c *WeaviateContainer) GrpcHostAddress(ctx context.Context) (string, error) {
grpcHostAddress, err := c.getHostAddress(ctx, grpcPort)
port, err := c.MappedPort(ctx, grpcPort)
if err != nil {
return "", err
return "", fmt.Errorf("failed to get container port: %w", err)
}
return grpcHostAddress, nil

host, err := c.Host(ctx)
if err != nil {
return "", fmt.Errorf("failed to get container host")
}

return fmt.Sprintf("%s:%s", host, port.Port()), nil
}

0 comments on commit e2dd72b

Please sign in to comment.