/
server.go
219 lines (184 loc) 路 5.87 KB
/
server.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
package mockaroo
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"time"
// gorilla seems like the best fit, supports a lot of rich matching
// and plays well with native golang http
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
const (
// write to the file/create if needed/if exists append
logFileFlag = os.O_RDWR | os.O_CREATE | os.O_APPEND
// default mode for log file creation
logFileMode = os.FileMode(0755)
)
//MockServer encapsulates a full mockaroo server
type MockServer interface {
//Start starts the mock server and if the server cannot be started
//returns an error
Start() error
}
//muxServer users gorilla mux for routing
type muxServer struct {
conf *Config
router *mux.Router
reqLogFile *os.File
}
// NewServer creates a mock server with the given configuration
func NewServer(conf *Config) MockServer {
return &muxServer{conf: conf, router: mux.NewRouter()}
}
func (s *muxServer) getRouter() *mux.Router {
return s.router
}
func (s *muxServer) Start() error {
if s.conf == nil {
return fmt.Errorf("server config is nil cannot start mockaroo")
}
// add all the required routes
s.addRoutes()
lfp := s.conf.ServerConfig.RequestLogPath
if lfp != nil {
// if logging path is configured setup logging
lf, err := s.setupLogFile()
if err != nil {
return fmt.Errorf("error logging to %v: %w", *lfp, err)
}
s.reqLogFile = lf
// the request log file will be closed when when the
// server shuts down
defer lf.Close()
}
// add all middlewares
s.router.Use(s.requestLoggingMiddleware)
// add the not found handler for logging
s.router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
log.Warnf("request path :%v lead to 404", req.RequestURI)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]interface{}{
"message": "request not found",
})
})
// let the router handle all the requests
http.Handle("/", s.router)
// start the server
// if the server fails to start it will return an error
if s.conf.ServerConfig.Mode == HTTPS {
return http.ListenAndServeTLS(*s.conf.ServerConfig.ListenAddr,
*s.conf.ServerConfig.SnakeOilCertPath,
*s.conf.ServerConfig.SnakeOilKeyPath, nil)
}
return http.ListenAndServe(*s.conf.ServerConfig.ListenAddr, nil)
}
type RequestLog struct {
RequestUri *string `json:"uri"`
Timestamp *time.Time `json:"request_time"`
Headers http.Header `json:"headers"`
Method *string `json:"method"`
ContentLength int64 `json:"content_length"`
RemoteAddr *string `json:"remote_addr"`
QueryValues *url.Values `json:"query_params"`
}
// take a http request and convert it into loggable entry
func requestLogFromRequest(r *http.Request) *RequestLog {
q := r.URL.Query()
t := time.Now().UTC()
pt := &t
return &RequestLog{
RequestUri: &r.RequestURI,
Timestamp: pt,
Headers: r.Header,
Method: &r.Method,
ContentLength: r.ContentLength,
RemoteAddr: &r.RemoteAddr,
QueryValues: &q,
}
}
func (s *muxServer) setupLogFile() (*os.File, error) {
lfp := s.conf.ServerConfig.RequestLogPath
lf, err := os.OpenFile(*lfp, logFileFlag, logFileMode)
if err != nil {
return nil, err
}
return lf, nil
}
// log all requests to STDOUT and specified log file if configured
func (s *muxServer) requestLoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO: add error handling here
rl, _ := json.Marshal(requestLogFromRequest(r))
if s.reqLogFile != nil {
fmt.Fprintf(s.reqLogFile, "%s\n", rl)
}
// call next handler
next.ServeHTTP(w, r)
})
}
func (s *muxServer) addRoutes() {
for _, m := range s.conf.ServerConfig.Mocks {
r := s.router.HandleFunc(m.Request.NormalizedPath, genHandleFunc(m)).Methods(*m.Request.Verb)
// if headers are present add them to the route
if m.Request.Headers != nil {
for k, v := range m.Request.Headers {
r.HeadersRegexp(k, v)
}
}
// if query params are present add them as well
if m.Request.Queries != nil {
for k, v := range m.Request.Queries {
r.Queries(k, v)
}
}
}
}
// generate the handle function for each mock
func genHandleFunc(mock *Mock) func(http.ResponseWriter, *http.Request) {
return func(resp http.ResponseWriter, req *http.Request) {
log.Infof("request matched mock:\"%v\" with path:\"%v\"", mock.Name, *mock.Request.Path)
// parse form if needed
err := req.ParseForm()
if err != nil {
resp.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(resp, "parsing form data failed error:%v", err)
}
// delay if we need to
if mock.Response.Delay != nil {
randomDelay := int64(0)
if mock.Response.Delay.MaxMillis-mock.Response.Delay.MinMillis > 0 {
randomDelay = mock.Response.Delay.MaxMillis - mock.Response.Delay.MinMillis
}
sleepFor := mock.Response.Delay.MinMillis + randomDelay
time.Sleep(time.Duration(sleepFor) * time.Millisecond)
}
for key, val := range mock.Response.Headers {
resp.Header().Add(key, val)
}
// write the status
resp.WriteHeader(mock.Response.Status)
switch {
case mock.Response.Template != nil:
// TODO: pass all context data here
err := mock.Response.Template.Execute(resp, NewTemplateContext(req))
if err != nil {
// raise a 500
errMsg := fmt.Sprintf("template execution failed for mock \"%v\" error:%v", mock.Name, err.Error())
log.Errorf("%s", errMsg)
resp.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(resp, errMsg)
}
case mock.Response.Content != nil:
fmt.Fprintf(resp, "%s", mock.Response.Content)
default:
// we should never be here if we are here mockaroo bunged it
// please open an issue
resp.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(resp, "BAD BAD BAD mockaroo fix your test cases; mock \"%v\"", mock.Name)
}
}
}