-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
167 lines (135 loc) · 3.6 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
163
164
165
166
167
//go:generate go-bindata fixture/resources.json fixture/spec.yml
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"github.com/piquette/finance-mock/fixture"
"github.com/piquette/finance-mock/server"
yaml "gopkg.in/yaml.v2"
)
const (
defaultPort = 12111
)
// verbose tracks whether the program is operating in verbose mode
var verbose bool
// This is set to the actual version by GoReleaser (using `-ldflags "-X ..."`)
// as it's run. Versions built from source will always show master.
var version = "master"
func main() {
var showVersion bool
var port int
var fixturesPath string
var specPath string
var unix string
flag.IntVar(&port, "port", defaultPort, "Port to listen on")
flag.StringVar(&fixturesPath, "fixtures", "", "Path to fixtures to use instead of bundled version")
flag.StringVar(&specPath, "spec", "", "Path to spec to use instead of bundled version")
flag.StringVar(&unix, "unix", "", "Unix socket to listen on")
flag.BoolVar(&verbose, "verbose", false, "Enable verbose mode")
flag.BoolVar(&showVersion, "version", false, "Show version and exit")
flag.Parse()
if showVersion || len(flag.Args()) == 1 && flag.Arg(0) == "version" {
fmt.Printf("%s\n", version)
return
}
if unix != "" && port != 0 {
flag.Usage()
abort("Specify only one of -port or -unix\n")
}
// Get spec.
spec, err := getSpec(specPath)
if err != nil {
abort(err.Error())
}
// Get fixtures.
fixtures, err := getFixtures(fixturesPath)
if err != nil {
abort(err.Error())
}
// Stub server.
stub := server.StubServer{Fixtures: fixtures, Spec: spec}
server.Version = version
server.Verbose = verbose
// Initialize server router.
err = stub.InitRouter()
if err != nil {
abort(fmt.Sprintf("Error initializing router: %v\n", err))
}
// Set handler.
http.HandleFunc("/", stub.HandleRequest)
http.HandleFunc("/config/", stub.HandleConfigRequest)
server := http.Server{}
// Init listener.
listener, err := getListener(port, unix)
if err != nil {
abort(err.Error())
}
// Serve.
server.Serve(listener)
}
func abort(message string) {
fmt.Fprintf(os.Stderr, message)
os.Exit(1)
}
func getListener(port int, unix string) (net.Listener, error) {
var err error
var listener net.Listener
if unix != "" {
listener, err = net.Listen("unix", unix)
fmt.Printf("Listening on unix socket %v\n", unix)
} else {
if port == 0 {
port = defaultPort
}
listener, err = net.Listen("tcp", ":"+strconv.Itoa(port))
fmt.Printf("Listening on port %v\n", port)
}
if err != nil {
return nil, fmt.Errorf("error listening on socket: %v\n", err)
}
return listener, nil
}
func getSpec(specPath string) (*fixture.Spec, error) {
var data []byte
var err error
if specPath == "" {
// Load the spec information from go-bindata.
data, err = Asset("fixture/spec.yml")
} else {
data, err = ioutil.ReadFile(specPath)
}
if err != nil {
return nil, fmt.Errorf("error loading spec: %v\n", err)
}
var spec fixture.Spec
err = yaml.Unmarshal(data, &spec)
if err != nil {
return nil, fmt.Errorf("error decoding spec: %v\n", err)
}
return &spec, nil
}
func getFixtures(fixturesPath string) (*fixture.Fixtures, error) {
var data []byte
var err error
if fixturesPath == "" {
// Load resources from go-bindata.
data, err = Asset("fixture/resources.json")
} else {
data, err = ioutil.ReadFile(fixturesPath)
}
if err != nil {
return nil, fmt.Errorf("error loading fixtures: %v\n", err)
}
var fixtures fixture.Fixtures
err = json.Unmarshal(data, &fixtures)
if err != nil {
return nil, fmt.Errorf("error decoding fixtures: %v\n", err)
}
return &fixtures, nil
}