/
proxy_send.go
173 lines (147 loc) · 4.48 KB
/
proxy_send.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"strings"
"github.com/google/go-querystring/query"
"github.com/grokify/gotilla/fmt/fmtutil"
"github.com/grokify/gotilla/io/ioutilmore"
"github.com/grokify/gotilla/net/httputilmore"
"github.com/grokify/gotilla/type/stringsutil"
"github.com/jessevdk/go-flags"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
"github.com/grokify/chathooks/src/config"
"github.com/grokify/chathooks/src/models"
)
const (
EnvWebhookUrlGlip = "GLIP_WEBHOOK"
EnvWebhookUrlSlack = "SLACK_WEBHOOK"
EnvChathooksReqInputType = "CHATHOOKS_REQ_INPUT_TYPE"
EnvChathooksReqOutputType = "CHATHOOKS_REQ_OUTPUT_TYPE"
EnvChathooksReqToken = "CHATHOOKS_REQ_TOKEN"
EnvChathooksReqURL = "CHATHOOKS_REQ_URL"
)
type cliOptions struct {
UrlOrGuid string `short:"u" long:"url" description:"Webhook URL or GUID" required:"true"`
Input string `short:"i" long:"input" description:"Input Service"`
Output string `short:"o" long:"output" description:"Output Adapter" required:"true"`
Token string `short:"t" long:"token" description:"Token"`
ChathooksURL string `short:"c" long:"chathooks_url" description:"Chathooks URL"`
}
type ExampleWebhookSender struct {
DocHandlersDir string
BaseUrl string
RequestParams models.RequestParams
}
func (s *ExampleWebhookSender) SendExamplesForInputType(inputType string) error {
rx := regexp.MustCompile(`^event-example_.+\.(json|txt)$`)
inputTypeDir := path.Join(s.DocHandlersDir, inputType)
files, err := ioutilmore.DirEntriesReSizeGt0(inputTypeDir, rx)
if err != nil {
return err
}
if len(files) == 0 {
return errors.New(fmt.Sprintf("no ^event-example_ files found for %v", inputTypeDir))
}
for _, file := range files {
filepath := path.Join(inputTypeDir, file.Name())
err := s.SendExampleForFilepath(filepath, inputType)
if err != nil {
return err
}
}
return nil
}
func BuildURLQueryString(baseUrl string, qry interface{}) string {
v, _ := query.Values(qry)
qryString := v.Encode()
if len(qryString) > 0 {
return baseUrl + "?" + qryString
}
return baseUrl
}
func (s *ExampleWebhookSender) SendExampleForFilepath(filepath string, inputType string) error {
bytes, err := ioutil.ReadFile(filepath)
if err != nil {
return err
}
qry := models.RequestParams{
InputType: inputType,
OutputType: s.RequestParams.OutputType,
Token: s.RequestParams.Token,
URL: s.RequestParams.URL}
fullUrl := BuildURLQueryString(s.BaseUrl, qry)
fmt.Printf("FULL_URL: %v\n", fullUrl)
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
req.SetBody(bytes)
req.Header.SetRequestURI(fullUrl)
req.Header.SetMethod("POST")
req.Header.Set(httputilmore.HeaderContentType, httputilmore.ContentTypeAppJsonUtf8)
fastClient := fasthttp.Client{}
err = fastClient.Do(req, resp)
fmt.Printf("RES_STATUS: %v\n", resp.StatusCode())
if resp.StatusCode() >= 300 || 1 == 1 {
fmt.Printf("RES_BODY: %v\n", string(resp.Body()))
}
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
return err
}
func main() {
opts := cliOptions{}
_, err := flags.Parse(&opts)
if err != nil {
log.Fatal(err)
}
qry := models.RequestParams{
InputType: opts.Input,
OutputType: opts.Output,
Token: opts.Token,
URL: opts.UrlOrGuid}
fmtutil.PrintJSON(qry)
if len(os.Getenv("ENV_PATH")) > 0 {
err := godotenv.Load(os.Getenv("ENV_PATH"))
if err != nil {
panic(err)
}
if len(os.Getenv(EnvChathooksReqInputType)) > 0 {
qry.InputType = os.Getenv(EnvChathooksReqInputType)
}
if len(os.Getenv(EnvChathooksReqOutputType)) > 0 {
qry.OutputType = os.Getenv(EnvChathooksReqOutputType)
}
if len(os.Getenv(EnvChathooksReqToken)) > 0 {
qry.Token = os.Getenv(EnvChathooksReqToken)
}
if len(os.Getenv(EnvChathooksReqURL)) > 0 {
qry.URL = os.Getenv(EnvChathooksReqURL)
}
}
fmtutil.PrintJSON(qry)
chathooksUrl := "http://localhost:8080/hook"
if len(strings.TrimSpace(opts.ChathooksURL)) > 0 {
chathooksUrl = opts.ChathooksURL
}
sender := ExampleWebhookSender{
DocHandlersDir: config.DocsHandlersDir(),
BaseUrl: chathooksUrl,
RequestParams: qry}
if len(sender.RequestParams.URL) == 0 {
sender.RequestParams.URL = os.Getenv(EnvWebhookUrlGlip)
}
examples := stringsutil.SplitCondenseSpace(qry.InputType, ",")
for _, ex := range examples {
err := sender.SendExamplesForInputType(strings.ToLower(ex))
if err != nil {
panic(err)
}
}
fmt.Println("DONE")
}