-
Notifications
You must be signed in to change notification settings - Fork 2
/
invoke.go
111 lines (88 loc) · 2.27 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
)
func handler(w http.ResponseWriter, r *http.Request) {
log.Print("invoke: received a request")
path := r.URL.Path
input := strings.Trim(path, "/")
ctx, cancel := context.WithTimeout(r.Context(), 1*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "/app/hw", input)
stdout, err := cmd.StdoutPipe()
if err != nil {
writeResponse(w, 500, err.Error())
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
writeResponse(w, 500, err.Error())
return
}
if err := cmd.Start(); err != nil {
writeResponse(w, 500, err.Error())
return
}
// fmt.Fprintf(stdin, "%s\n", input) // io.WriteString(stdin, input)
seout, _ := ioutil.ReadAll(stderr)
stout, _ := ioutil.ReadAll(stdout)
if err := cmd.Wait(); err != nil {
writeResponse(w, 500, string(seout))
return
}
if len(seout) > 0 {
writeResponse(w, 400, string(seout))
return
}
writeResponse(w, 200, string(stout))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", handler)
logHandler := loggingHandler(mux)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("invoker: listening on %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), logHandler))
}
type statusRecorder struct {
http.ResponseWriter
status int
byteCount int
}
func (rec *statusRecorder) WriteHeader(code int) {
rec.status = code
rec.ResponseWriter.WriteHeader(code)
}
func (rec *statusRecorder) Write(p []byte) (int, error) {
bc, err := rec.ResponseWriter.Write(p)
rec.byteCount += bc
return bc, err
}
func loggingHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
rec := statusRecorder{w, 200, 0}
next.ServeHTTP(&rec, req)
remoteAddr := req.Header.Get("X-Forwarded-For")
if remoteAddr == "" {
remoteAddr = req.RemoteAddr
}
ua := req.Header.Get("User-Agent")
log.Printf("%s - \"%s %s %s\" (%s) %d %d \"%s\"", remoteAddr, req.Method, req.URL.Path, req.Proto, req.Host, rec.status, rec.byteCount, ua)
})
}
func writeResponse(w http.ResponseWriter, code int, message string) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(code)
w.Write([]byte(message))
}