main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright (c) 2019 Docker Inc.
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED,
  14. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH
  22. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. package main
  25. import (
  26. "context"
  27. "fmt"
  28. "net"
  29. "os"
  30. v1 "github.com/docker/api/backend/v1"
  31. "github.com/docker/api/client"
  32. "github.com/docker/api/server"
  33. _ "github.com/gogo/googleapis/google/rpc"
  34. "github.com/gogo/protobuf/types"
  35. "github.com/pkg/errors"
  36. "github.com/sirupsen/logrus"
  37. "github.com/urfave/cli"
  38. )
  39. func main() {
  40. app := cli.NewApp()
  41. app.Name = "example"
  42. app.Usage = "example backend"
  43. app.Description = ""
  44. app.UseShortOptionHandling = true
  45. app.EnableBashCompletion = true
  46. app.Flags = []cli.Flag{
  47. cli.BoolFlag{
  48. Name: "debug",
  49. Usage: "enable debug output in the logs",
  50. },
  51. cli.StringFlag{
  52. Name: "address,a",
  53. Usage: "address of the server",
  54. },
  55. }
  56. app.Before = func(clix *cli.Context) error {
  57. if clix.GlobalBool("debug") {
  58. logrus.SetLevel(logrus.DebugLevel)
  59. }
  60. return nil
  61. }
  62. app.Action = func(clix *cli.Context) error {
  63. ctx, cancel := client.NewContext()
  64. defer cancel()
  65. // create a new GRPC server with the provided server package
  66. s := server.New()
  67. // listen on a socket to accept connects
  68. l, err := net.Listen("unix", clix.GlobalString("address"))
  69. if err != nil {
  70. return errors.Wrap(err, "listen unix socket")
  71. }
  72. defer l.Close()
  73. // create our instance of the backend server implementation
  74. backend := &backend{}
  75. // register our instance with the GRPC server
  76. v1.RegisterBackendServer(s, backend)
  77. // handle context being closed or canceled
  78. go func() {
  79. <-ctx.Done()
  80. logrus.Info("backend signaled to stop")
  81. s.Stop()
  82. }()
  83. logrus.WithField("address", clix.GlobalString("address")).Info("serving daemon API")
  84. // start the GRPC server to serve on the listener
  85. return s.Serve(l)
  86. }
  87. if err := app.Run(os.Args); err != nil {
  88. fmt.Fprintln(os.Stderr, err)
  89. os.Exit(1)
  90. }
  91. }
  92. type backend struct {
  93. }
  94. func (b *backend) BackendInformation(ctx context.Context, _ *types.Empty) (*v1.BackendInformationResponse, error) {
  95. return &v1.BackendInformationResponse{
  96. ID: "com.docker.api.backend.example.v1",
  97. }, nil
  98. }