-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (129 loc) Β· 3.45 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"sort"
"sync"
"time"
"github.com/fatih/color"
)
type Config struct {
Method string `json:"method"`
URL string `json:"url"`
RequestBody interface{} `json:"requestBody"`
RequestCount int `json:"requestCount"`
Authorization string `json:"authorization"`
ShowResponseBody bool `json:"showResponseBody"`
Timeout int `json:"timeout"`
}
type Result struct {
ID int
Method string
URL string
StatusCode int
ResponseBody string
Err error
}
var (
configFile string
config Config
results []Result
mu sync.Mutex
)
func init() {
flag.StringVar(&configFile, "config", "", "Path to YAML configuration file")
}
func main() {
flag.Parse()
if configFile == "" {
log.Fatal("Configuration file is required")
}
err := loadConfig(configFile, &config)
if err != nil {
log.Fatalf("Error loading config: %v\n", err)
}
client := &http.Client{
Timeout: time.Duration(config.Timeout) * time.Second,
Transport: &http.Transport{MaxConnsPerHost: 50},
}
var wg sync.WaitGroup
wg.Add(config.RequestCount)
for i := 1; i < config.RequestCount+1; i++ {
go func(i int) {
defer wg.Done()
sendRequest(client, i)
}(i)
}
wg.Wait()
sortResults()
printResults()
}
func loadConfig(filename string, config *Config) error {
data, err := os.ReadFile(filename)
if err != nil {
return err
}
err = json.Unmarshal(data, config)
if err != nil {
return err
}
return nil
}
func sendRequest(client *http.Client, id int) {
// requestBody := bytes.NewBuffer([]byte(fmt.Sprintf("%v", config.RequestBody)))
request, err := http.NewRequest(config.Method, config.URL, nil)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", config.Authorization)
response, err := client.Do(request)
if err != nil {
storeResult(Result{ID: id, Method: config.Method, URL: config.URL, Err: err})
return
}
defer response.Body.Close()
var bodyStr string
if config.ShowResponseBody {
body, err := io.ReadAll(response.Body)
if err != nil {
storeResult(Result{ID: id, Method: config.Method, URL: config.URL, StatusCode: response.StatusCode, Err: err})
return
}
bodyStr = string(body)
}
storeResult(Result{ID: id, Method: config.Method, URL: config.URL, StatusCode: response.StatusCode, ResponseBody: bodyStr})
}
func storeResult(result Result) {
mu.Lock()
results = append(results, result)
mu.Unlock()
}
func sortResults() {
sort.Slice(results, func(i, j int) bool {
return results[i].ID < results[j].ID
})
}
func printResults() {
for _, result := range results {
if result.Err != nil {
color.Red("Request %d failed: %v\n", result.ID, result.Err)
} else {
switch {
case result.StatusCode >= 200 && result.StatusCode < 300:
color.Green("%d: [%s] %s, %d\n", result.ID, result.Method, result.URL, result.StatusCode)
case result.StatusCode >= 300 && result.StatusCode < 400:
color.Yellow("%d: [%s] %s, %d\n", result.ID, result.Method, result.URL, result.StatusCode)
case result.StatusCode >= 400 && result.StatusCode < 600:
color.Red("%d: [%s] %s, %d\n", result.ID, result.Method, result.URL, result.StatusCode)
default:
color.White("%d: [%s] %s, %d\n", result.ID, result.Method, result.URL, result.StatusCode)
}
}
if config.ShowResponseBody && result.ResponseBody != "" {
fmt.Println(result.ResponseBody)
}
}
}