forked from jmartin82/mmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmock.go
127 lines (108 loc) · 3.91 KB
/
mmock.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
package main
import (
"errors"
"flag"
"fmt"
"log"
"net"
"path/filepath"
"strings"
"github.com/jmartin82/mmock/console"
"github.com/jmartin82/mmock/definition"
"github.com/jmartin82/mmock/match"
"github.com/jmartin82/mmock/parse"
"github.com/jmartin82/mmock/parse/fakedata"
"github.com/jmartin82/mmock/route"
"github.com/jmartin82/mmock/server"
"github.com/jmartin82/mmock/translate"
)
//ErrNotFoundDefaultPath if we can't resolve the current path
var ErrNotFoundDefaultPath = errors.New("We can't determinate the current path")
//ErrNotFoundAnyMock when we don't found any valid mock definition to load
var ErrNotFoundAnyMock = errors.New("No valid mock definition found")
func banner() {
fmt.Println("MMock v 0.0.1")
fmt.Println("")
fmt.Print(
` .---. .---.
: : o : me want request!
_..-: o : :-.._ /
.-'' ' ` + "`" + `---' ` + "`" + `---' " ` + "`" + `` + "`" + `-.
.' " ' " . " . ' " ` + "`" + `.
: '.---.,,.,...,.,.,.,..---. ' ;
` + "`" + `. " ` + "`" + `. .' " .'
` + "`" + `. '` + "`" + `. .' ' .'
` + "`" + `. ` + "`" + `-._ _.-' " .' .----.
` + "`" + `. " '"--...--"' . ' .' .' o ` + "`" + `.
.'` + "`" + `-._' " . " _.-'` + "`" + `. : o :
.' ` + "`" + `` + "`" + `` + "`" + `--.....--''' ' ` + "`" + `:_ o :
.' " ' " " ; ` + "`" + `.;";";";'
; ' " ' . ; .' ; ; ;
; ' ' ' " .' .-'
' " " ' " " _.-'
`)
fmt.Println("")
}
// Get preferred outbound ip of this machine
func getOutboundIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return "127.0.0.1"
}
defer conn.Close()
log.Println("Getting external IP")
localAddr := conn.LocalAddr().String()
idx := strings.LastIndex(localAddr, ":")
return localAddr[0:idx]
}
func getRouter(mocks []definition.Mock, dUpdates chan []definition.Mock) *route.RequestRouter {
log.Printf("Loding router with %d definitions\n", len(mocks))
return &route.RequestRouter{Mocks: mocks, Matcher: match.MockMatch{}, DUpdates: dUpdates}
}
func startServer(ip string, port int, done chan bool, router route.Router, mLog chan definition.Match) {
filler := parse.FakeDataParse{fakedata.FakeAdapter{}}
dispatcher := server.Dispatcher{ip, port, router, translate.HTTPTranslator{}, filler, mLog}
dispatcher.Start()
done <- true
}
func startConsole(ip string, port int, done chan bool, mLog chan definition.Match) {
dispatcher := console.Dispatcher{Ip: ip, Port: port, Mlog: mLog}
dispatcher.Start()
done <- true
}
func main() {
banner()
outIP := getOutboundIP()
path, err := filepath.Abs("./config")
if err != nil {
panic(ErrNotFoundDefaultPath)
}
sIP := flag.String("server-ip", outIP, "Mock server IP")
sPort := flag.Int("server-port", 8082, "Mock Server Port")
cIP := flag.String("console-ip", outIP, "Console Server IP")
cPort := flag.Int("cconsole-port", 8083, "Console server Port")
cPath := flag.String("config-path", path, "Mocks definition folder")
console := flag.Bool("console", true, "Console enabled (true/false)")
flag.Parse()
//chanels
mLog := make(chan definition.Match)
dUpdates := make(chan []definition.Mock)
done := make(chan bool)
path, _ = filepath.Abs(*cPath)
log.Printf("Reading Mock definition from: %s\n", path)
definitionReader := definition.FileDefinition{path, dUpdates}
mocks := definitionReader.ReadMocksDefinition()
if len(mocks) == 0 {
log.Fatalln(ErrNotFoundAnyMock.Error())
}
definitionReader.WatchDir()
router := getRouter(mocks, dUpdates)
router.MockChangeWatch()
go startServer(*cIP, *cPort, done, router, mLog)
log.Printf("HTTP Server running at %s:%d\n", *cIP, *cPort)
if *console {
go startConsole(*sIP, *sPort, done, mLog)
log.Printf("Console running at %s:%d\n", *sIP, *sPort)
}
<-done
}