github.com/khulnasoft-lab/khulnasoft@v26.0.1-0.20240328202558-330a6f959fe0+incompatible/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 "time" 10 ) 11 12 type start struct { 13 File string 14 } 15 16 func main() { 17 l, err := net.Listen("unix", "/run/docker/plugins/plugin.sock") 18 if err != nil { 19 panic(err) 20 } 21 22 mux := http.NewServeMux() 23 mux.HandleFunc("/LogDriver.StartLogging", func(w http.ResponseWriter, req *http.Request) { 24 startReq := &start{} 25 if err := json.NewDecoder(req.Body).Decode(startReq); err != nil { 26 http.Error(w, err.Error(), http.StatusBadRequest) 27 return 28 } 29 30 f, err := os.OpenFile(startReq.File, os.O_RDONLY, 0o600) 31 if err != nil { 32 http.Error(w, err.Error(), http.StatusInternalServerError) 33 return 34 } 35 36 // Close the file immediately, this allows us to test what happens in the daemon when the plugin has closed the 37 // file or, for example, the plugin has crashed. 38 f.Close() 39 40 w.WriteHeader(http.StatusOK) 41 fmt.Fprintln(w, `{}`) 42 }) 43 server := http.Server{ 44 Addr: l.Addr().String(), 45 Handler: mux, 46 ReadHeaderTimeout: 2 * time.Second, // This server is not for production code; picked an arbitrary timeout to statisfy gosec (G112: Potential Slowloris Attack) 47 } 48 49 server.Serve(l) 50 }