-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.go
152 lines (125 loc) · 2.99 KB
/
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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/signal"
"strings"
"github.com/zeebe-io/zbc-go/zbc"
"github.com/zeebe-io/zbc-go/zbc/models/zbmsgpack"
"github.com/zeebe-io/zbc-go/zbc/models/zbsubscriptions"
"github.com/zeebe-io/zbc-go/zbc/services/zbsubscribe"
)
func main() {
brokerAddr := os.Getenv("BROKER")
if brokerAddr == "" {
brokerAddr = "0.0.0.0:51015"
}
topicName := os.Getenv("TOPIC")
if topicName == "" {
topicName = "default-topic"
}
zbClient, err := zbc.NewClient(brokerAddr)
if err != nil {
panic("Failed to connect to " + brokerAddr)
}
subscription, err := zbClient.JobSubscription(topicName, "http-go", "http", 1000, 32, handleJob)
if err != nil {
panic("Unable to open subscription")
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
err := subscription.Close()
if err != nil {
panic("Failed to close subscription")
}
fmt.Println("Closed subscription")
os.Exit(0)
}()
subscription.Start()
}
func handleJob(client zbsubscribe.ZeebeAPI, event *zbsubscriptions.SubscriptionEvent) {
job, _ := event.GetJob()
url := getParameter(job, "url")
if url == nil {
fmt.Println("Missing required parameter 'URL'")
job.Retries--
client.FailJob(event)
return
}
method := getParameter(job, "method")
if method == nil {
method = "GET"
}
var reqBody io.Reader
body := getParameter(job, "body")
if body != nil {
jsonDocument, err := json.Marshal(body)
if err != nil {
fmt.Println(err)
job.Retries--
client.FailJob(event)
return
}
reqBody = bytes.NewReader(jsonDocument)
}
statusCode, resBody, err := request(url.(string), method.(string), reqBody)
if err != nil {
fmt.Println(err)
job.Retries--
client.FailJob(event)
return
}
result := make(map[string]interface{})
result["statusCode"] = statusCode
result["body"] = resBody
job.SetPayload(result)
client.CompleteJob(event)
}
func getParameter(job *zbmsgpack.Job, param string) interface{} {
value := job.CustomHeader[param]
if value != nil {
return value
}
payload, _ := job.GetPayload()
value = (*payload)[param]
return value
}
func request(url string, method string, body io.Reader) (int, interface{}, error) {
req, _ := http.NewRequest(method, url, body)
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return 0, nil, err
}
statusCode := res.StatusCode
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return 0, nil, err
}
if hasContentType(*res, "application/json") {
var jsonDocument interface{}
err := json.Unmarshal(resBody, &jsonDocument)
if err != nil {
return statusCode, resBody, err
}
return statusCode, jsonDocument, nil
}
return statusCode, string(resBody), nil
}
func hasContentType(res http.Response, cType string) bool {
contentType := res.Header["Content-Type"]
for _, c := range contentType {
if strings.Contains(c, cType) {
return true
}
}
return false
}