main.go 986 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "os"
  8. )
  9. type start struct {
  10. File string
  11. }
  12. func main() {
  13. l, err := net.Listen("unix", "/run/docker/plugins/plugin.sock")
  14. if err != nil {
  15. panic(err)
  16. }
  17. mux := http.NewServeMux()
  18. mux.HandleFunc("/LogDriver.StartLogging", func(w http.ResponseWriter, req *http.Request) {
  19. startReq := &start{}
  20. if err := json.NewDecoder(req.Body).Decode(startReq); err != nil {
  21. http.Error(w, err.Error(), http.StatusBadRequest)
  22. return
  23. }
  24. f, err := os.OpenFile(startReq.File, os.O_RDONLY, 0600)
  25. if err != nil {
  26. http.Error(w, err.Error(), http.StatusInternalServerError)
  27. return
  28. }
  29. // Close the file immediately, this allows us to test what happens in the daemon when the plugin has closed the
  30. // file or, for example, the plugin has crashed.
  31. f.Close()
  32. w.WriteHeader(http.StatusOK)
  33. fmt.Fprintln(w, `{}`)
  34. })
  35. server := http.Server{
  36. Addr: l.Addr().String(),
  37. Handler: mux,
  38. }
  39. server.Serve(l)
  40. }