serve.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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()
  39. contextsService := server.NewContexts()
  40. containersv1.RegisterContainersServer(s, p)
  41. streamsv1.RegisterStreamingServer(s, p)
  42. contextsv1.RegisterContextsServer(s, contextsService)
  43. go func() {
  44. <-ctx.Done()
  45. logrus.Info("stopping server")
  46. s.Stop()
  47. }()
  48. logrus.WithField("address", opts.address).Info("serving daemon API")
  49. // start the GRPC server to serve on the listener
  50. return s.Serve(listener)
  51. }