This repository was archived by the owner on Jun 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblueprint.go
More file actions
122 lines (98 loc) · 2.84 KB
/
Copy pathblueprint.go
File metadata and controls
122 lines (98 loc) · 2.84 KB
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
package mockgopher
import (
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
)
// Blueprint represent how should be serve the routes
type Blueprint struct {
Host string
Port int
Delay *int64
Routes []*Route
Log io.Writer
ResourceLocator ResourceLocator
}
// NewBlueprint creates a new instance with some default values
func NewBlueprint(host string, port int) *Blueprint {
var delay *int64
delay = new(int64)
*delay = 0
return &Blueprint{host, port, delay, []*Route{}, NewStdout(), nil}
}
// AddRoute is a simple way to add new routes to a created blueprint
func (b *Blueprint) AddRoute(path string, method string, body string) *Route {
methods := []string{method}
route := &Route{
Request: &Request{
Path: path,
Methods: methods,
},
Response: &Response{
Template: body,
Status: 200,
},
}
b.Routes = append(b.Routes, route)
return route
}
// MakeRouter create a router using the blueprint routes
func (b *Blueprint) MakeRouter() *mux.Router {
router := mux.NewRouter()
for _, route := range b.Routes {
route := route // make a copy of the route for use in the lambda
hpLen := len(route.Request.Headers) * 2
hPairs := make([]string, hpLen, hpLen)
// Convert Headers{Key, Value} to pair Headers slice (How Mux uses headers)
for index, header := range route.Request.Headers {
hPairs[index*2] = header.Key
hPairs[(index*2)+1] = header.Value
}
router.HandleFunc(route.Request.Path, func(w http.ResponseWriter, r *http.Request) {
var delay int64
delay = 0
if route.Response.Delay != nil {
delay = *route.Response.Delay
} else if b.Delay != nil {
delay = *b.Delay
}
b.Log.Write([]byte(fmt.Sprintf(
"%s - %s - %s - Delay: %dms\n", r.Method, r.Host, r.RequestURI, delay)))
time.Sleep(time.Duration(delay) * time.Millisecond)
for _, header := range route.Response.Headers {
w.Header().Set(header.Key, header.Value)
}
if len(route.Response.Resources) >= 1 {
res := route.Response.Resources
ran := rand.New(rand.NewSource(time.Now().Unix()))
fileContent := b.ResourceLocator.Locate(res[ran.Intn(len(res))])
w.Write(fileContent)
} else {
output, err := View(string(b.ResourceLocator.Locate(route.Response.Template)))
if err != nil {
log.Fatalln(err)
}
w.WriteHeader(200)
fmt.Fprintf(w, output)
}
}).Methods(route.Request.Methods...).HeadersRegexp(hPairs...)
}
return router
}
// Stdout is used to print stuff in the standar output implementing io.Writer
type Stdout struct {
stdout *os.File
}
// NewStdout return a Stdout object thats use the standar output
func NewStdout() *Stdout {
return &Stdout{os.Stdout}
}
// Write to standar output as string
func (s *Stdout) Write(p []byte) (n int, err error) {
return fmt.Fprint(s.stdout, string(p))
}