-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
79 lines (71 loc) · 2.06 KB
/
handler.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
package main
import (
"os"
"os/exec"
"io"
"io/ioutil"
"log"
"bytes"
"strings"
"errors"
"encoding/json"
)
type message struct {
Handler string `json:"handler"`
}
func getHandlers(handlerdir string) []string {
handlers, err := getFilenames(handlerdir)
failOnError(err, "Could not find any handlers in handlerdir")
log.Printf("[getHandlers] Found handlers: %q", handlers)
return handlers
}
func parseMsg(msg []byte) message {
brd := bytes.NewReader(msg)
dec := json.NewDecoder(brd)
var m message
for {
if err := dec.Decode(&m); err == io.EOF {
break
} else if err != nil {
failOnError(err, "Failed to decode message's json payload")
}
}
return m
}
// Returns a path to the handler if it exists in the handlerdir, else err
func checkHandlerExists(handlerdir string, handler string) (string, error) {
handlers := getHandlers(handlerdir)
for _, h := range handlers {
log.Printf("[checkHandlerExists] checking %q == %q", h, handler)
if h == handler {
s := []string{handlerdir, handler}
sj := strings.Join(s, "/")
log.Printf("[checkHandlerExists] %q == %q is true, returning handlerpath of %q", h, handler, sj)
return sj, nil
}
}
return "", errors.New("handler doesn't exist")
}
func handleMsg(handlerdir string, msg []byte) {
parsedmsg := parseMsg(msg)
h, err := checkHandlerExists(handlerdir, parsedmsg.Handler)
var output []byte
if err == nil {
tmpfile, err := ioutil.TempFile(os.TempDir(), parsedmsg.Handler)
failOnError(err, "Failed to create a tempfile")
ioutil.WriteFile(tmpfile.Name(), msg, 0644)
log.Printf("[handleMsg] wrote msg to %q", tmpfile.Name())
log.Printf("[handleMsg] running to '%s %s'", h, tmpfile.Name())
cmd := exec.Command(h, tmpfile.Name())
stdoutpipe, err := cmd.StdoutPipe()
stderrpipe, err := cmd.StderrPipe()
go logCmdOutputStream(stdoutpipe)
go logCmdOutputStream(stderrpipe)
cmd.Run()
if err != nil {
log.Fatal(err)
}
}
log.Printf("[handleMsg:MSG] %s", msg)
log.Printf("[handleMsg:OUT] %s", output)
}