-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
289 lines (232 loc) · 5.81 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/bobmaertz/canner/config"
"github.com/google/uuid"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
var (
seed = func() int64 {
return time.Now().UnixNano()
}
)
type Matcher struct {
Request config.Request
Response config.Response
}
func init() {
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
log.SetLevel(log.TraceLevel)
log.SetFormatter(&log.TextFormatter{
DisableColors: false,
FullTimestamp: true,
})
// Set the file name of the configurations file
viper.SetConfigName("config.yml")
// Set the path to look for the configurations file
viper.AddConfigPath("./conf")
// Enable VIPER to read Environment Variables
viper.AutomaticEnv()
viper.SetConfigType("yml")
}
func main() {
var c config.Configurations
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Error reading config file, %s", err)
}
err = viper.Unmarshal(&c)
if err != nil {
log.Fatalf("unable to decode into struct, %v", err)
}
rtr := mux.NewRouter()
// handle http first
// process all http requests and parse by url
// we need to create a map with all the possible responses matched to each request.
matchers := createMatchers(c)
for path, matcher := range matchers {
rtr.HandleFunc(path, createHandler(matcher))
}
http.Handle("/", rtr)
// Set default values for timeouts if not set by configs
readTimeout := 15 * time.Second
if c.Server.ReadTimeout != 0 {
readTimeout = c.Server.ReadTimeout
}
writeTimeout := 15 * time.Second
if c.Server.WriteTimeout != 0 {
writeTimeout = c.Server.WriteTimeout
}
idleTimeout := 60 * time.Second
if c.Server.IdleTimeout != 0 {
idleTimeout = c.Server.IdleTimeout
}
readHeaderTimeout := 15 * time.Second
if c.Server.ReadHeaderTimeout != 0 {
readHeaderTimeout = c.Server.ReadHeaderTimeout
}
log.Infof("Listening on %d\n", c.Server.Port)
srv := &http.Server{
Handler: rtr,
Addr: fmt.Sprintf("localhost:%d", c.Server.Port),
WriteTimeout: writeTimeout,
ReadTimeout: readTimeout,
ReadHeaderTimeout: readHeaderTimeout,
IdleTimeout: idleTimeout,
}
log.Fatal(srv.ListenAndServe())
}
func createHandler(matchers []Matcher) func(w http.ResponseWriter, incoming *http.Request) {
return func(w http.ResponseWriter, incoming *http.Request) {
trace := uuid.New().String()
log.Infof("Request [id: %s]: %v", trace, incoming)
//Todo check hijack parameter
writer, err := OpenWriter(w)
if err != nil {
log.Errorf("error creating writer: %v\n", err)
return
}
var m *Matcher
for _, r := range matchers {
if !methodMatches(r.Request.Method, incoming.Method) {
log.Error("method does not match")
continue
}
if !headersMatch(r.Request.Headers, incoming.Header) {
log.Error("header does not match")
continue
}
if !bodyMatches(r.Request.Body, incoming.Body) {
log.Error("body does not match")
continue
}
//Set floating matcher var to the matcher we found
m = &r
}
if m != nil {
if m.Response.Latency != nil {
waitFor(*m.Response.Latency, time.Sleep)
}
//Everything passed; return value
for k, v := range m.Response.Headers {
writer.Header().Add(k, v)
}
writer.WriteHeader(m.Response.StatusCode)
_, err := writer.Write([]byte(m.Response.Body))
if err != nil {
return
}
log.Infof("Response [id: %s]: %v", trace, m.Response)
return
}
writer.WriteHeader(http.StatusNotFound)
_, err = writer.Write([]byte("mock not found"))
if err != nil {
return
}
}
}
type ZombieWriter struct {
rawWriter http.ResponseWriter
hijacker http.Hijacker
}
func OpenWriter(w http.ResponseWriter) (http.ResponseWriter, error) {
var hj http.Hijacker
if _, ok := w.(*ZombieWriter); ok {
hj, ok = w.(http.Hijacker)
if !ok {
return nil, errors.New("hijack not supported")
}
}
return &ZombieWriter{rawWriter: w, hijacker: hj}, nil
}
func (z *ZombieWriter) Header() http.Header {
return z.rawWriter.Header()
}
func (z *ZombieWriter) Write(in []byte) (int, error) {
//todo add hijack param
if z.hijacker != nil {
conn, _, err := z.hijacker.Hijack()
if err != nil {
return -100, err
}
defer conn.Close()
//TODO: hijack the connection and write the response
return 0, nil
}
// if not hijacked, continue as normal
return z.rawWriter.Write(in)
}
func (z *ZombieWriter) WriteHeader(header int) {
z.rawWriter.WriteHeader(header)
}
func waitFor(latency config.LatencyConfig, sleep func(time.Duration)) {
switch latency.Type {
case "random":
rand.Seed(seed())
max := latency.Delay.Nanoseconds()
s := rand.Intn(int(max))
sleep(time.Duration(s) * time.Nanosecond)
case "simple":
sleep(latency.Delay)
default:
//none
}
}
func methodMatches(reqd string, incoming string) bool {
if reqd == "" {
reqd = "GET" //default to GET
}
if reqd == strings.ToUpper(incoming) {
return true
}
return false
}
func headersMatch(reqd map[string]string, hdrs http.Header) bool {
if len(reqd) == 0 {
return true //no headers to match
}
for k, v := range reqd {
val := hdrs.Get(k)
if val == v {
return true
}
}
return false
}
func bodyMatches(reqBody string, incomingBody io.ReadCloser) bool {
if reqBody == "" {
return true //no body to match
}
body, err := io.ReadAll(incomingBody)
if err != nil {
log.Errorf("Error reading body: %v", err)
return false
}
if string(body) == reqBody {
return true
}
return false
}
func createMatchers(c config.Configurations) map[string][]Matcher {
matchers := make(map[string][]Matcher)
for _, m := range c.Matchers {
mtchr := Matcher{
Request: m.Request,
Response: m.Response,
}
matchers[m.Request.Path] = append(matchers[m.Request.Path], mtchr)
}
return matchers
}