-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
138 lines (120 loc) · 2.61 KB
/
app.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
package main
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"strings"
"time"
)
// App struct
type App struct {
ctx context.Context
client *http.Client
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 50 * time.Second,
}
a.client = &http.Client{
Timeout: 50 * time.Second,
Transport: tr,
}
a.ctx = ctx
}
func makeRequest(c *http.Client,
r *http.Request) ([]byte, *http.Response, error) {
resp, err := c.Do(r)
if err != nil {
return nil, resp, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, resp, err
}
return body, resp, nil
}
func HeadersToStr(h *http.Header) string {
headersArr := []string{}
for k, v := range *h {
vals := strings.Join(v, "; ")
headersArr = append(headersArr, k+": "+vals)
}
result := strings.Join(headersArr, "\n")
return result
}
type RequestResult struct {
Method string `json:"Method"`
URL string `json:"URL"`
ReqHeaders string `json:"ReqHeaders"`
RequestBody string `json:"RequestBody"`
Body string `json:"Body"`
HeadersStr string `json:"HeadersStr"`
Error string `json:"Error"`
}
func (a *App) RunCurl(curl string) RequestResult {
res := RequestResult{}
r, ok := Parse(curl)
if !ok {
res.Error = "Unable to parse curl"
return res
}
res = a.MakeRequest(r.Url, r.Method, r.Body, r.Header)
res.ReqHeaders = r.Header.ToString()
res.Method = r.Method
res.URL = r.Url
res.RequestBody = r.Body
return res
}
type Header struct {
Key string
value string
}
// Greet returns a greeting for the given name
func (a *App) MakeRequest(
urlIn string,
method string,
body string,
headers Headers,
) RequestResult {
result := RequestResult{
URL: urlIn,
Method: method,
RequestBody: body,
}
rbody := bytes.NewBuffer([]byte(body))
r, err := http.NewRequest(method, urlIn, rbody)
if err != nil {
result.Error = err.Error()
return result
}
for key, value := range headers {
r.Header.Add(key, value)
}
res, httpResp, err := makeRequest(a.client, r)
if err != nil {
result.Error = err.Error()
return result
}
result.HeadersStr = HeadersToStr(&httpResp.Header)
b := bytes.NewBuffer(make([]byte, 0, len(res)))
err = json.Indent(b, res, "\n", " ")
if err != nil {
return RequestResult{
Body: string(res),
Error: err.Error(),
}
}
texts := string(b.Bytes())
result.Body = texts
return result
}