proxy.go 903 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package proxy
  2. import (
  3. "context"
  4. "sync"
  5. "github.com/docker/api/client"
  6. containersv1 "github.com/docker/api/protos/containers/v1"
  7. streamsv1 "github.com/docker/api/protos/streams/v1"
  8. )
  9. type clientKey struct{}
  10. // WithClient adds the client to the context
  11. func WithClient(ctx context.Context, c *client.Client) (context.Context, error) {
  12. return context.WithValue(ctx, clientKey{}, c), nil
  13. }
  14. // Client returns the client from the context
  15. func Client(ctx context.Context) *client.Client {
  16. c, _ := ctx.Value(clientKey{}).(*client.Client)
  17. return c
  18. }
  19. // Proxy implements the gRPC server and forwards the actions
  20. // to the right backend
  21. type Proxy interface {
  22. containersv1.ContainersServer
  23. streamsv1.StreamingServer
  24. }
  25. type proxy struct {
  26. mu sync.Mutex
  27. streams map[string]*Stream
  28. }
  29. // New creates a new proxy server
  30. func New() Proxy {
  31. return &proxy{
  32. streams: map[string]*Stream{},
  33. }
  34. }