proxy.go 1.4 KB

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