-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
qdrant.go
85 lines (69 loc) · 2.27 KB
/
qdrant.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package qdrant
import (
"context"
"fmt"
"time"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
// QdrantContainer represents the Qdrant container type used in the module
type QdrantContainer struct {
testcontainers.Container
}
// RunContainer creates an instance of the Qdrant container type
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*QdrantContainer, error) {
req := testcontainers.ContainerRequest{
Image: "qdrant/qdrant:v1.7.4",
ExposedPorts: []string{"6333/tcp", "6334/tcp"},
WaitingFor: wait.ForAll(
wait.ForListeningPort("6333/tcp").WithStartupTimeout(5*time.Second),
wait.ForListeningPort("6334/tcp").WithStartupTimeout(5*time.Second),
),
}
genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
}
for _, opt := range opts {
if err := opt.Customize(&genericContainerReq); err != nil {
return nil, err
}
}
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
if err != nil {
return nil, err
}
return &QdrantContainer{Container: container}, nil
}
// RESTEndpoint returns the REST endpoint of the Qdrant container
func (c *QdrantContainer) RESTEndpoint(ctx context.Context) (string, error) {
containerPort, err := c.MappedPort(ctx, "6333/tcp")
if err != nil {
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.Sprintf("http://%s:%s", host, containerPort.Port()), nil
}
// GRPCEndpoint returns the gRPC endpoint of the Qdrant container
func (c *QdrantContainer) GRPCEndpoint(ctx context.Context) (string, error) {
containerPort, err := c.MappedPort(ctx, "6334/tcp")
if err != nil {
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.Sprintf("%s:%s", host, containerPort.Port()), nil
}
// WebUI returns the web UI endpoint of the Qdrant container
func (c *QdrantContainer) WebUI(ctx context.Context) (string, error) {
s, err := c.RESTEndpoint(ctx)
if err != nil {
return "", err
}
return s + "/dashboard", nil
}