-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (134 loc) · 3.33 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
package main
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/sync/semaphore"
)
var Port int
var Watch bool
var AllowOrigin string
var collection MockCollection
func main() {
var rootCmd = &cobra.Command{
Use: "jmock <path to mocks>",
Short: "Simple and easy to use json/post API mock server",
Long: `Simple and easy to use json/post API mock server`,
Version: "0.2.0",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(args[0])
files, err := filepath.Glob(args[0])
if err != nil {
log.Fatal(err)
}
err = collection.Rebuild(files)
if err != nil {
log.Fatal(err)
}
if Watch {
go watchAndRebuildCollection(files)
}
http.HandleFunc("/", errorHandler(httpHandler))
server := &http.Server{
Addr: fmt.Sprintf(":%d", Port),
ReadHeaderTimeout: 3 * time.Second,
}
err = server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
},
}
rootCmd.Flags().IntVarP(&Port, "port", "p", 9090, "Specify port to listen")
rootCmd.Flags().BoolVarP(&Watch, "watch", "w", false, "Watch for file changes")
rootCmd.Flags().StringVarP(&AllowOrigin, "allow-origin", "o", "*", "Set up Access-Control-Allow-Origin header")
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
os.Exit(1)
}
}
func watchAndRebuildCollection(files []string) {
log.Printf("Watching for %d files\n", len(files))
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
sem := semaphore.NewWeighted(1)
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
log.Fatal("Watcher`s event failed")
}
if event.Op.String() != "WRITE" {
continue
}
if !sem.TryAcquire(1) {
continue
}
go func() {
defer sem.Release(1)
log.Println("Changes detected. Updating mocks...")
time.Sleep(100 * time.Millisecond)
errRebuild := collection.Rebuild(files)
if errRebuild != nil {
log.Println("Error while rebuilding collection:\n", errRebuild)
}
}()
case errWatch, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("Watch error:", errWatch)
}
}
}()
for _, f := range files {
err = watcher.Add(f)
if err != nil {
log.Fatal(err)
}
}
<-done
}
func httpHandler(w http.ResponseWriter, r *http.Request) error {
body := getBodyCopy(r)
mock := collection.Lookup(r)
r.Body = io.NopCloser(bytes.NewBuffer(body))
if AllowOrigin != "" {
w.Header().Set("Access-Control-Allow-Origin", AllowOrigin)
}
if mock == nil {
log.Printf("Mock not found for request: %s %s\n", r.Method, r.URL.Path)
w.WriteHeader(501)
return nil
}
return errors.Wrap(ProcessMock(w, r, mock), mock.Name)
}
func getBodyCopy(req *http.Request) []byte {
bodyBytes, _ := io.ReadAll(req.Body)
req.Body.Close()
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
return bodyBytes
}
func errorHandler(f func(http.ResponseWriter, *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := f(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("Error %q: %v", r.RequestURI, err)
}
}
}