github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/integration/plugin/logging/cmd/close_on_start/main.go (about)

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