-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (41 loc) · 829 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
)
var args []string
func handler(w http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
cmd := exec.CommandContext(r.Context(), args[0], args[1:]...)
cmd.Stdin = r.Body
cmd.Stdout = buf
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
io.WriteString(w, err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
err = cmd.Wait()
if err != nil {
log.Printf(err.Error())
w.WriteHeader(http.StatusInternalServerError)
}
io.Copy(w, buf)
}
func main() {
args = os.Args[1:]
log.Printf("start server")
log.Printf("args: %+v", args)
http.HandleFunc("/", handler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}