client.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. client *http.Client
  27. }
  28. // NewClient creates a Desktop integration client for the provided in-memory
  29. // socket address (AF_UNIX or named pipe).
  30. func NewClient(apiEndpoint string) *Client {
  31. var transport http.RoundTripper = &http.Transport{
  32. DisableCompression: true,
  33. DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
  34. return memnet.DialEndpoint(ctx, apiEndpoint)
  35. },
  36. }
  37. transport = otelhttp.NewTransport(transport)
  38. c := &Client{
  39. client: &http.Client{Transport: transport},
  40. }
  41. return c
  42. }
  43. // Close releases any open connections.
  44. func (c *Client) Close() error {
  45. c.client.CloseIdleConnections()
  46. return nil
  47. }
  48. type PingResponse struct {
  49. ServerTime int64 `json:"serverTime"`
  50. }
  51. // Ping is a minimal API used to ensure that the server is available.
  52. func (c *Client) Ping(ctx context.Context) (*PingResponse, error) {
  53. req, err := http.NewRequestWithContext(ctx, http.MethodGet, backendURL("/ping"), http.NoBody)
  54. if err != nil {
  55. return nil, err
  56. }
  57. resp, err := c.client.Do(req)
  58. if err != nil {
  59. return nil, err
  60. }
  61. defer func() {
  62. _ = resp.Body.Close()
  63. }()
  64. if resp.StatusCode != http.StatusOK {
  65. return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
  66. }
  67. var ret PingResponse
  68. if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
  69. return nil, err
  70. }
  71. return &ret, nil
  72. }
  73. // backendURL generates a URL for the given API path.
  74. //
  75. // NOTE: Custom transport handles communication. The host is to create a valid
  76. // URL for the Go http.Client that is also descriptive in error/logs.
  77. func backendURL(path string) string {
  78. return "http://docker-desktop/" + strings.TrimPrefix(path, "/")
  79. }