-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoke.go
49 lines (43 loc) · 981 Bytes
/
invoke.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
package main
import (
"strings"
"log"
"net/http"
"os"
"os/exec"
)
func main() {
http.HandleFunc("/", scriptHandler)
// Determine port for HTTP service.
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
// Start HTTP server.
log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
func scriptHandler(w http.ResponseWriter, r *http.Request) {
COMMAND:=os.Getenv("COMMAND")
RUN_SCRIPT:=os.Getenv("RUN_SCRIPT")
if COMMAND == "" {
cmd := exec.CommandContext(r.Context(), "python", "gcp/print_env.py")
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
w.WriteHeader(500)
}
w.Write(out)
} else if strings.Contains(COMMAND,"bash") {
cmd := exec.CommandContext(r.Context(), COMMAND,"-c", RUN_SCRIPT)
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
w.WriteHeader(500)
}
w.Write(out)
}
}