-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
72 lines (57 loc) · 1.83 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
package main
import (
"flag"
"fmt"
"net/http"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func httpHandleFuncWithLogs(pattern string, handler func(http.ResponseWriter, *http.Request)) {
wrappedHandler := func(w http.ResponseWriter, r *http.Request) {
LogRequest(r)
handler(w, r)
}
http.HandleFunc(pattern, wrappedHandler)
}
func setZeroLogLevel(logLevel string) {
selectedLevel := zerolog.DebugLevel
switch logLevel {
case "debug":
selectedLevel = zerolog.DebugLevel
case "info":
selectedLevel = zerolog.InfoLevel
case "warn":
selectedLevel = zerolog.WarnLevel
case "error":
selectedLevel = zerolog.ErrorLevel
case "fatal":
selectedLevel = zerolog.FatalLevel
case "panic":
selectedLevel = zerolog.PanicLevel
}
fmt.Println("set log level:", selectedLevel)
zerolog.SetGlobalLevel(selectedLevel)
zerolog.TimestampFieldName = "timestamp"
log.Logger = log.With().Str("type", "gozzmock").Logger()
}
func main() {
var initExpectations string
flag.StringVar(&initExpectations, "expectations", "[]", "set initial expectations")
var logLevel string
flag.StringVar(&logLevel, "loglevel", "debug", "set log level: debug, info, warn, error, fatal, panic")
flag.Parse()
fmt.Println("initial expectations:", initExpectations)
fmt.Println("loglevel:", logLevel)
fmt.Println("tail:", flag.Args())
setZeroLogLevel(logLevel)
exps := ExpectationsFromString(initExpectations)
for _, exp := range exps {
ControllerAddExpectation(exp.Key, exp, nil)
}
http.HandleFunc("/gozzmock/status", HandlerStatus)
httpHandleFuncWithLogs("/gozzmock/add_expectation", HandlerAddExpectation)
httpHandleFuncWithLogs("/gozzmock/remove_expectation", HandlerRemoveExpectation)
httpHandleFuncWithLogs("/gozzmock/get_expectations", HandlerGetExpectations)
httpHandleFuncWithLogs("/", HandlerDefault)
http.ListenAndServe(":8080", nil)
}