forked from rehearse/rehearse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
65 lines (53 loc) · 1.46 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
)
const (
VERSION = "0.2.0"
)
var options struct {
path string
address string
port int
stubsPath string
printVersion bool
}
func main() {
var err error
flag.StringVar(&options.path, "path", "", "Optional path to serve static files from.")
flag.StringVar(&options.address, "address", "127.0.0.1", "Address to listen on.")
flag.IntVar(&options.port, "port", 3333, "Port to listen on.")
flag.StringVar(&options.stubsPath, "stubs", "", "Optional path to JSON file to preload stubs.")
flag.BoolVar(&options.printVersion, "version", false, "Print version and exit.")
flag.Parse()
if options.printVersion {
fmt.Printf("rehearse v%s\n", VERSION)
os.Exit(0)
}
stubHandler := NewStubHandler()
if options.path != "" {
stubHandler.fallbackHandler = http.FileServer(http.Dir(options.path))
}
if options.stubsPath != "" {
func() {
file, err := os.Open(options.stubsPath)
if err != nil {
log.Fatalf("Unable to open %s: %v", options.stubsPath, err)
}
defer file.Close()
if err := stubHandler.load(file); err != nil {
log.Fatalf("Unable to load %s: %v", options.stubsPath, err)
}
}()
}
http.Handle("/", stubHandler)
log.Printf("Starting rehearse on %s:%d", options.address, options.port)
err = http.ListenAndServe(fmt.Sprintf("%s:%d", options.address, options.port), stubHandler)
if err != nil {
log.Fatalf("Unable to start server: %v\n", err)
}
}