-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathapplication.go
174 lines (134 loc) · 4.18 KB
/
application.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
package main
import (
"crypto/md5"
_ "expvar"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/quii/mockingjay-server/mockingjay"
"github.com/quii/mockingjay-server/monkey"
)
var (
// ErrCDCFail describes when a fake server is not compatible with a given URL
ErrCDCFail = fmt.Errorf("at least one endpoint was incompatible with the real URL supplied")
)
type mockingjayLoader func(closer io.ReadCloser) ([]mockingjay.FakeEndpoint, error)
type compatabilityChecker interface {
CheckCompatibility(endpoints []mockingjay.FakeEndpoint, realURL string) bool
}
type serverMaker func([]mockingjay.FakeEndpoint, bool, io.Writer) *mockingjay.Server
type monkeyServerMaker func(http.Handler, string) (http.Handler, error)
type application struct {
configLoader configLoader
mockingjayLoader mockingjayLoader
compatabilityChecker compatabilityChecker
mockingjayServerMaker serverMaker
monkeyServerMaker monkeyServerMaker
logger *log.Logger
mjServer *mockingjay.Server
configPath string
monkeyConfigPath string
yamlMD5 [md5.Size]byte
}
func defaultApplication(logger *log.Logger, httpTimeout time.Duration, configPath string) (app *application) {
app = new(application)
app.configPath = configPath
if isURL(configPath) {
app.configLoader = urlLoader{}
} else {
app.configLoader = globFileLoader{}
}
app.mockingjayLoader = mockingjay.NewFakeEndpoints
app.compatabilityChecker = mockingjay.NewCompatabilityChecker(logger, httpTimeout)
app.mockingjayServerMaker = mockingjay.NewServer
app.monkeyServerMaker = monkey.NewServer
app.logger = logger
return
}
func (a *application) PollConfig() {
if _, pollable := a.configLoader.(pollable); pollable {
for range time.Tick(time.Millisecond * 500) {
endpoints, err := a.loadConfig()
if err != nil {
log.Println(err)
} else {
a.mjServer.Endpoints = endpoints
}
}
} else {
a.logger.Println("config loader is not pollable, restart app to update config")
}
}
// CreateServer will create a fake server from the configuration found in configPath with optional performance constraints from configutation found in monkeyConfigPath
func (a *application) CreateServer(monkeyConfigPath string, debugMode bool, disablePolling bool) (server http.Handler, err error) {
a.monkeyConfigPath = monkeyConfigPath
endpoints, err := a.loadConfig()
if err != nil || len(endpoints) == 0 {
return
}
return a.createFakeServer(endpoints, debugMode, disablePolling)
}
// CheckCompatibility will run a MJ config against a realURL to see if it's compatible
func (a *application) CheckCompatibility(realURL string) error {
endpoints, err := a.loadConfig()
if err != nil {
return err
}
if a.compatabilityChecker.CheckCompatibility(endpoints, realURL) {
a.logger.Println("All endpoints are compatible")
return nil
}
return ErrCDCFail
}
func (a *application) loadConfig() (endpoints []mockingjay.FakeEndpoint, err error) {
configs, _, err := a.configLoader.Load(a.configPath)
if err != nil {
return
}
for _, conf := range configs {
mjEndpoint, err := a.mockingjayLoader(conf)
if err != nil {
return nil, err
}
conf.Close()
endpoints = append(endpoints, mjEndpoint...)
}
return
}
/*
Giovanni Bajo [5:09 PM]
it doesn't respect the semantics of io.Writer
[5:09]
like you wouldn’t be able to compose it with a gzip.Writer
[5:10]
so I’m not sure you’re doing yourself a favor in implementing the io.Writer interface, it’s prone to mistakes
*/
type fileUpdater struct {
path string
}
func (fu *fileUpdater) Write(p []byte) (n int, err error) {
f, err := os.Create(fu.path)
if err != nil {
return 0, err
}
n, err = f.Write(p)
_ = f.Sync()
return
}
func (a *application) createFakeServer(endpoints []mockingjay.FakeEndpoint, debugMode bool, disablePolling bool) (server http.Handler, err error) {
if !disablePolling {
go a.PollConfig()
}
configFile := fileUpdater{a.configPath}
a.mjServer = a.mockingjayServerMaker(endpoints, debugMode, &configFile)
monkeyServer, err := a.monkeyServerMaker(a.mjServer, a.monkeyConfigPath)
if err != nil {
return nil, err
}
router := http.NewServeMux()
router.Handle("/", monkeyServer)
return router, nil
}