client.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Copyright 2024 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 desktop
  14. import (
  15. "context"
  16. "encoding/json"
  17. "fmt"
  18. "io"
  19. "net"
  20. "net/http"
  21. "strings"
  22. "github.com/docker/compose/v2/internal"
  23. "github.com/docker/compose/v2/internal/memnet"
  24. "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
  25. )
  26. // identify this client in the logs
  27. var userAgent = "compose/" + internal.Version
  28. // Client for integration with Docker Desktop features.
  29. type Client struct {
  30. apiEndpoint string
  31. client *http.Client
  32. }
  33. // NewClient creates a Desktop integration client for the provided in-memory
  34. // socket address (AF_UNIX or named pipe).
  35. func NewClient(apiEndpoint string) *Client {
  36. var transport http.RoundTripper = &http.Transport{
  37. DisableCompression: true,
  38. DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
  39. return memnet.DialEndpoint(ctx, apiEndpoint)
  40. },
  41. }
  42. transport = otelhttp.NewTransport(transport)
  43. return &Client{
  44. apiEndpoint: apiEndpoint,
  45. client: &http.Client{Transport: transport},
  46. }
  47. }
  48. func (c *Client) Endpoint() string {
  49. return c.apiEndpoint
  50. }
  51. // Close releases any open connections.
  52. func (c *Client) Close() error {
  53. c.client.CloseIdleConnections()
  54. return nil
  55. }
  56. type PingResponse struct {
  57. ServerTime int64 `json:"serverTime"`
  58. }
  59. // Ping is a minimal API used to ensure that the server is available.
  60. func (c *Client) Ping(ctx context.Context) (*PingResponse, error) {
  61. req, err := c.newRequest(ctx, http.MethodGet, "/ping", http.NoBody)
  62. if err != nil {
  63. return nil, err
  64. }
  65. resp, err := c.client.Do(req)
  66. if err != nil {
  67. return nil, err
  68. }
  69. defer func() {
  70. _ = resp.Body.Close()
  71. }()
  72. if resp.StatusCode != http.StatusOK {
  73. return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
  74. }
  75. var ret PingResponse
  76. if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
  77. return nil, err
  78. }
  79. return &ret, nil
  80. }
  81. type FeatureFlagResponse map[string]FeatureFlagValue
  82. type FeatureFlagValue struct {
  83. Enabled bool `json:"enabled"`
  84. }
  85. func (c *Client) FeatureFlags(ctx context.Context) (FeatureFlagResponse, error) {
  86. req, err := c.newRequest(ctx, http.MethodGet, "/features", http.NoBody)
  87. if err != nil {
  88. return nil, err
  89. }
  90. resp, err := c.client.Do(req)
  91. if err != nil {
  92. return nil, err
  93. }
  94. defer func() {
  95. _ = resp.Body.Close()
  96. }()
  97. if resp.StatusCode != http.StatusOK {
  98. return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
  99. }
  100. var ret FeatureFlagResponse
  101. if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
  102. return nil, err
  103. }
  104. return ret, nil
  105. }
  106. func (c *Client) newRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error) {
  107. req, err := http.NewRequestWithContext(ctx, method, backendURL(path), body)
  108. if err != nil {
  109. return nil, err
  110. }
  111. req.Header.Set("User-Agent", userAgent)
  112. return req, nil
  113. }
  114. // backendURL generates a URL for the given API path.
  115. //
  116. // NOTE: Custom transport handles communication. The host is to create a valid
  117. // URL for the Go http.Client that is also descriptive in error/logs.
  118. func backendURL(path string) string {
  119. return "http://docker-desktop/" + strings.TrimPrefix(path, "/")
  120. }