MCPcopy Create free account
hub / github.com/beefsack/webify / ServeHTTP

Method ServeHTTP

lib/server.go:19–119  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

17}
18
19func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
20 // Start subprocess
21 cmd := exec.Command(s.Script[0], s.Script[1:]...)
22
23 // Get handles to subprocess stdin, stdout and stderr
24 stdinPipe, err := cmd.StdinPipe()
25 if err != nil {
26 log.Printf("error accessing subprocess stdin: %v", err)
27 respError(w)
28 return
29 }
30 defer stdinPipe.Close()
31 stderrPipe, err := cmd.StderrPipe()
32 if err != nil {
33 log.Printf("error accessing subprocess stderr: %v", err)
34 respError(w)
35 return
36 }
37 stdoutPipe, err := cmd.StdoutPipe()
38 if err != nil {
39 log.Printf("error accessing subprocess stdout: %v", err)
40 respError(w)
41 return
42 }
43
44 // Start the subprocess
45 err = cmd.Start()
46 if err != nil {
47 log.Printf("error starting subprocess: %v", err)
48 respError(w)
49 return
50 }
51
52 // We use a WaitGroup to wait for all goroutines to finish
53 wg := sync.WaitGroup{}
54
55 // Write request body to subprocess stdin
56 wg.Add(1)
57 go func() {
58 defer func() {
59 stdinPipe.Close()
60 wg.Done()
61 }()
62 _, err = io.Copy(stdinPipe, r.Body)
63 if err != nil {
64 log.Printf("error writing request body to subprocess stdin: %v", err)
65 respError(w)
66 return
67 }
68 }()
69
70 // Read all stderr and write to parent stderr if not empty
71 wg.Add(1)
72 go func() {
73 defer wg.Done()
74 stderr, err := ioutil.ReadAll(stderrPipe)
75 if err != nil {
76 log.Printf("error reading subprocess stderr: %v", err)

Callers

nothing calls this directly

Calls 1

respErrorFunction · 0.85

Tested by

no test coverage detected