proxy.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package proxy
  14. import (
  15. "context"
  16. "sync"
  17. "github.com/docker/compose-cli/api/client"
  18. "github.com/docker/compose-cli/config"
  19. containersv1 "github.com/docker/compose-cli/protos/containers/v1"
  20. contextsv1 "github.com/docker/compose-cli/protos/contexts/v1"
  21. streamsv1 "github.com/docker/compose-cli/protos/streams/v1"
  22. volumesv1 "github.com/docker/compose-cli/protos/volumes/v1"
  23. "github.com/docker/compose-cli/server/proxy/streams"
  24. )
  25. type clientKey struct{}
  26. // WithClient adds the client to the context
  27. func WithClient(ctx context.Context, c *client.Client) context.Context {
  28. return context.WithValue(ctx, clientKey{}, c)
  29. }
  30. // Client returns the client from the context
  31. func Client(ctx context.Context) *client.Client {
  32. c, _ := ctx.Value(clientKey{}).(*client.Client)
  33. return c
  34. }
  35. // Proxy implements the gRPC server and forwards the actions
  36. // to the right backend
  37. type Proxy interface {
  38. containersv1.ContainersServer
  39. streamsv1.StreamingServer
  40. volumesv1.VolumesServer
  41. ContextsProxy() contextsv1.ContextsServer
  42. }
  43. type proxy struct {
  44. configDir string
  45. mu sync.Mutex
  46. streams map[string]*streams.Stream
  47. contextsProxy *contextsProxy
  48. }
  49. // New creates a new proxy server
  50. func New(ctx context.Context) Proxy {
  51. configDir := config.Dir(ctx)
  52. return &proxy{
  53. configDir: configDir,
  54. streams: map[string]*streams.Stream{},
  55. contextsProxy: &contextsProxy{
  56. configDir: configDir,
  57. },
  58. }
  59. }
  60. func (p *proxy) ContextsProxy() contextsv1.ContextsServer {
  61. return p.contextsProxy
  62. }