handler.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package sdk
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "os"
  8. )
  9. const activatePath = "/Plugin.Activate"
  10. // Handler is the base to create plugin handlers.
  11. // It initializes connections and sockets to listen to.
  12. type Handler struct {
  13. mux *http.ServeMux
  14. }
  15. // NewHandler creates a new Handler with an http mux.
  16. func NewHandler(manifest string) Handler {
  17. mux := http.NewServeMux()
  18. mux.HandleFunc(activatePath, func(w http.ResponseWriter, r *http.Request) {
  19. w.Header().Set("Content-Type", DefaultContentTypeV1_1)
  20. fmt.Fprintln(w, manifest)
  21. })
  22. return Handler{mux: mux}
  23. }
  24. // Serve sets up the handler to serve requests on the passed in listener
  25. func (h Handler) Serve(l net.Listener) error {
  26. server := http.Server{
  27. Addr: l.Addr().String(),
  28. Handler: h.mux,
  29. }
  30. return server.Serve(l)
  31. }
  32. // ServeTCP makes the handler to listen for request in a given TCP address.
  33. // It also writes the spec file in the right directory for docker to read.
  34. // Due to constrains for running Docker in Docker on Windows, data-root directory
  35. // of docker daemon must be provided. To get default directory, use
  36. // WindowsDefaultDaemonRootDir() function. On Unix, this parameter is ignored.
  37. func (h Handler) ServeTCP(pluginName, addr, daemonDir string, tlsConfig *tls.Config) error {
  38. l, spec, err := newTCPListener(addr, pluginName, daemonDir, tlsConfig)
  39. if err != nil {
  40. return err
  41. }
  42. if spec != "" {
  43. defer os.Remove(spec)
  44. }
  45. return h.Serve(l)
  46. }
  47. // ServeUnix makes the handler to listen for requests in a unix socket.
  48. // It also creates the socket file in the right directory for docker to read.
  49. func (h Handler) ServeUnix(addr string, gid int) error {
  50. l, spec, err := newUnixListener(addr, gid)
  51. if err != nil {
  52. return err
  53. }
  54. if spec != "" {
  55. defer os.Remove(spec)
  56. }
  57. return h.Serve(l)
  58. }
  59. // ServeWindows makes the handler to listen for request in a Windows named pipe.
  60. // It also creates the spec file in the right directory for docker to read.
  61. // Due to constrains for running Docker in Docker on Windows, data-root directory
  62. // of docker daemon must be provided. To get default directory, use
  63. // WindowsDefaultDaemonRootDir() function. On Unix, this parameter is ignored.
  64. func (h Handler) ServeWindows(addr, pluginName, daemonDir string, pipeConfig *WindowsPipeConfig) error {
  65. l, spec, err := newWindowsListener(addr, pluginName, daemonDir, pipeConfig)
  66. if err != nil {
  67. return err
  68. }
  69. if spec != "" {
  70. defer os.Remove(spec)
  71. }
  72. return h.Serve(l)
  73. }
  74. // HandleFunc registers a function to handle a request path with.
  75. func (h Handler) HandleFunc(path string, fn func(w http.ResponseWriter, r *http.Request)) {
  76. h.mux.HandleFunc(path, fn)
  77. }