/
main.go
187 lines (156 loc) · 4.72 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"golang.org/x/net/publicsuffix"
"bytes"
"crypto/tls"
b64 "encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"github.com/vorteil/direktiv-apps/pkg/direktivapps"
)
const code = "com.%s.error"
// request the input object for the requester container
type request struct {
Method string `json:"method"`
URL string `json:"url"`
Body interface{} `json:"body"`
Headers map[string]interface{} `json:"headers"`
Params map[string]interface{} `json:"params"`
Username string `json:"username"`
Password string `json:"password"`
InsecureSkipVerify bool `json:"insecureSkipVerify"`
}
// output for the requester container
type output struct {
Body interface{} `json:"body,omitempty"` // when the response is able to be unmarshalled
Headers http.Header `json:"headers"`
StatusCode int `json:"status-code"`
Status string `json:"status"`
}
func Request(w http.ResponseWriter, r *http.Request) {
obj := new(request)
aid, err := direktivapps.Unmarshal(obj, r)
if err != nil {
direktivapps.RespondWithError(w, fmt.Sprintf(code, "unmarshal-input"), err.Error())
return
}
var b []byte
direktivapps.Log(aid, "Creating cookie jar")
jar, err := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
if err != nil {
direktivapps.RespondWithError(w, fmt.Sprintf(code, "cookie-jar"), err.Error())
return
}
client := &http.Client{
Jar: jar,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: obj.InsecureSkipVerify,
},
},
}
direktivapps.Log(aid, "Creating new request")
if obj.Body != nil {
switch v := obj.Body.(type) {
case string:
direktivapps.Log(aid, "Body is a string ignore marshal.")
b = []byte(obj.Body.(string))
default:
direktivapps.Log(aid, fmt.Sprintf("Body is of type %v", v))
b, err = json.Marshal(obj.Body)
if err != nil {
direktivapps.RespondWithError(w, fmt.Sprintf(code, "marshal-body"), err.Error())
return
}
}
direktivapps.Log(aid, "Body exists, attaching to the request")
}
direktivapps.Log(aid, "Creating URL...")
u, err := url.Parse(obj.URL)
if err != nil {
direktivapps.RespondWithError(w, fmt.Sprintf(code, "url-parse"), err.Error())
return
}
q := u.Query()
for k, v := range obj.Params {
var actualVal string
// Handle other types provided and convert to string automatically
switch t := v.(type) {
case bool:
actualVal = strconv.FormatBool(t)
case float64:
actualVal = strconv.FormatFloat(t, 'f', 6, 64)
case string:
actualVal = t
}
direktivapps.Log(aid, fmt.Sprintf("Adding param %s=%s", k, actualVal))
q.Set(k, actualVal)
}
u.RawQuery = q.Encode()
req, err := http.NewRequest(obj.Method, u.String(), bytes.NewReader(b))
if err != nil {
direktivapps.RespondWithError(w, fmt.Sprintf(code, "create-request"), err.Error())
return
}
for k, v := range obj.Headers {
var actualVal string
// Handle other types provided and convert to string automatically
switch t := v.(type) {
case bool:
actualVal = strconv.FormatBool(t)
case float64:
actualVal = strconv.FormatFloat(t, 'f', 6, 64)
case string:
actualVal = t
}
// Adding a header requires it to be a string so might as well sprintf
req.Header.Add(k, actualVal)
}
if obj.Username != "" && obj.Password != "" {
sEnc := b64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", obj.Username, obj.Password)))
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", sEnc))
direktivapps.Log(aid, "Adding Basic authorization header")
}
direktivapps.Log(aid, "Sending request...")
resp, err := client.Do(req)
if err != nil {
direktivapps.RespondWithError(w, fmt.Sprintf(code, "send-request"), err.Error())
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
direktivapps.RespondWithError(w, fmt.Sprintf(code, "read-resp-body"), err.Error())
return
}
var mapBody map[string]interface{}
var dataBody interface{}
var responding output
responding.Status = resp.Status
responding.StatusCode = resp.StatusCode
responding.Headers = resp.Header
// if body is unable to be marshalled treat as a byte array
err = json.Unmarshal(body, &mapBody)
if err != nil {
json.Unmarshal(body, &dataBody)
responding.Body = dataBody
} else {
responding.Body = mapBody
}
data, err := json.Marshal(responding)
if err != nil {
direktivapps.RespondWithError(w, fmt.Sprintf(code, "marshal-output"), err.Error())
return
}
direktivapps.Respond(w, data)
}
func main() {
direktivapps.StartServer(Request)
}