serve.go 1.5 KB

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