serve.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package cmd
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. "github.com/sirupsen/logrus"
  6. "github.com/spf13/cobra"
  7. containersv1 "github.com/docker/api/protos/containers/v1"
  8. contextsv1 "github.com/docker/api/protos/contexts/v1"
  9. "github.com/docker/api/server"
  10. "github.com/docker/api/server/proxy"
  11. )
  12. type serveOpts struct {
  13. address string
  14. }
  15. // ServeCommand returns the command to serve the API
  16. func ServeCommand() *cobra.Command {
  17. // FIXME(chris-crone): Should warn that specified context is ignored
  18. var opts serveOpts
  19. cmd := &cobra.Command{
  20. Use: "serve",
  21. Short: "Start an api server",
  22. RunE: func(cmd *cobra.Command, args []string) error {
  23. return runServe(cmd.Context(), opts)
  24. },
  25. }
  26. cmd.Flags().StringVar(&opts.address, "address", "", "The address to listen to")
  27. return cmd
  28. }
  29. func runServe(ctx context.Context, opts serveOpts) error {
  30. s := server.New(ctx)
  31. listener, err := server.CreateListener(opts.address)
  32. if err != nil {
  33. return errors.Wrap(err, "listen address "+opts.address)
  34. }
  35. // nolint errcheck
  36. defer listener.Close()
  37. p := proxy.NewContainerAPI()
  38. contextsService := server.NewContexts()
  39. containersv1.RegisterContainersServer(s, p)
  40. contextsv1.RegisterContextsServer(s, contextsService)
  41. go func() {
  42. <-ctx.Done()
  43. logrus.Info("stopping server")
  44. s.Stop()
  45. }()
  46. logrus.WithField("address", opts.address).Info("serving daemon API")
  47. // start the GRPC server to serve on the listener
  48. return s.Serve(listener)
  49. }