-
Notifications
You must be signed in to change notification settings - Fork 2
/
dispatcher.go
54 lines (45 loc) · 1.03 KB
/
dispatcher.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
package main
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func checkPayload(payload string) string {
if string(payload[0]) == "@" {
payload_file := payload[1:]
file, err := os.OpenFile(payload_file, os.O_RDONLY, 0444)
if err != nil {
log.Println(err)
}
defer file.Close()
reader, _ := ioutil.ReadAll(file)
return string(reader)
}
return payload
}
func sendRequest(endpoint string, payload string) {
raw := bytes.NewBuffer([]byte(payload))
req, err := http.NewRequest("POST", endpoint, raw)
if err != nil {
log.Println("ERROR:", err)
return
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
log.Println("ERROR:", err)
return
}
log.Println("HTTP request sent", response)
}
func Dispatch(endpoint string, payload string) {
log.Println("Dispatching", endpoint, payload)
endpoint = strings.TrimSpace(endpoint)
payload = strings.TrimSpace(payload)
payload = checkPayload(payload)
sendRequest(endpoint, payload)
}