proxy.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. contextsv1 "github.com/docker/api/protos/contexts/v1"
  8. streamsv1 "github.com/docker/api/protos/streams/v1"
  9. "github.com/docker/api/server/proxy/streams"
  10. )
  11. type clientKey struct{}
  12. // WithClient adds the client to the context
  13. func WithClient(ctx context.Context, c *client.Client) (context.Context, error) {
  14. return context.WithValue(ctx, clientKey{}, c), nil
  15. }
  16. // Client returns the client from the context
  17. func Client(ctx context.Context) *client.Client {
  18. c, _ := ctx.Value(clientKey{}).(*client.Client)
  19. return c
  20. }
  21. // Proxy implements the gRPC server and forwards the actions
  22. // to the right backend
  23. type Proxy interface {
  24. containersv1.ContainersServer
  25. streamsv1.StreamingServer
  26. ContextsProxy() contextsv1.ContextsServer
  27. }
  28. type proxy struct {
  29. currentContext string
  30. mu sync.Mutex
  31. streams map[string]*streams.Stream
  32. contextsProxy *contextsProxy
  33. }
  34. // New creates a new proxy server
  35. func New(currentContext string) Proxy {
  36. return &proxy{
  37. currentContext: currentContext,
  38. streams: map[string]*streams.Stream{},
  39. contextsProxy: &contextsProxy{},
  40. }
  41. }
  42. func (p *proxy) ContextsProxy() contextsv1.ContextsServer {
  43. return p.contextsProxy
  44. }