client.go 3.3 KB

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