proxy.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/cli/server/proxy/streams"
  19. "github.com/docker/compose-cli/config"
  20. composev1 "github.com/docker/compose-cli/protos/compose/v1"
  21. containersv1 "github.com/docker/compose-cli/protos/containers/v1"
  22. contextsv1 "github.com/docker/compose-cli/protos/contexts/v1"
  23. streamsv1 "github.com/docker/compose-cli/protos/streams/v1"
  24. volumesv1 "github.com/docker/compose-cli/protos/volumes/v1"
  25. )
  26. type clientKey struct{}
  27. // WithClient adds the client to the context
  28. func WithClient(ctx context.Context, c *client.Client) context.Context {
  29. return context.WithValue(ctx, clientKey{}, c)
  30. }
  31. // Client returns the client from the context
  32. func Client(ctx context.Context) *client.Client {
  33. c, _ := ctx.Value(clientKey{}).(*client.Client)
  34. return c
  35. }
  36. // Proxy implements the gRPC server and forwards the actions
  37. // to the right backend
  38. type Proxy interface {
  39. composev1.ComposeServer
  40. containersv1.ContainersServer
  41. streamsv1.StreamingServer
  42. volumesv1.VolumesServer
  43. ContextsProxy() contextsv1.ContextsServer
  44. }
  45. type proxy struct {
  46. configDir string
  47. mu sync.Mutex
  48. streams map[string]*streams.Stream
  49. contextsProxy *contextsProxy
  50. }
  51. // New creates a new proxy server
  52. func New(ctx context.Context) Proxy {
  53. configDir := config.Dir(ctx)
  54. return &proxy{
  55. configDir: configDir,
  56. streams: map[string]*streams.Stream{},
  57. contextsProxy: &contextsProxy{
  58. configDir: configDir,
  59. },
  60. }
  61. }
  62. func (p *proxy) ContextsProxy() contextsv1.ContextsServer {
  63. return p.contextsProxy
  64. }