...
1
2
3
4
5 package gin
6
7 import (
8 "flag"
9 "io"
10 "os"
11
12 "github.com/gin-gonic/gin/binding"
13 )
14
15
16 const EnvGinMode = "GIN_MODE"
17
18 const (
19
20 DebugMode = "debug"
21
22 ReleaseMode = "release"
23
24 TestMode = "test"
25 )
26
27 const (
28 debugCode = iota
29 releaseCode
30 testCode
31 )
32
33
34
35
36
37
38
39
40
41 var DefaultWriter io.Writer = os.Stdout
42
43
44 var DefaultErrorWriter io.Writer = os.Stderr
45
46 var (
47 ginMode = debugCode
48 modeName = DebugMode
49 )
50
51 func init() {
52 mode := os.Getenv(EnvGinMode)
53 SetMode(mode)
54 }
55
56
57 func SetMode(value string) {
58 if value == "" {
59 if flag.Lookup("test.v") != nil {
60 value = TestMode
61 } else {
62 value = DebugMode
63 }
64 }
65
66 switch value {
67 case DebugMode:
68 ginMode = debugCode
69 case ReleaseMode:
70 ginMode = releaseCode
71 case TestMode:
72 ginMode = testCode
73 default:
74 panic("gin mode unknown: " + value + " (available mode: debug release test)")
75 }
76
77 modeName = value
78 }
79
80
81 func DisableBindValidation() {
82 binding.Validator = nil
83 }
84
85
86
87 func EnableJsonDecoderUseNumber() {
88 binding.EnableDecoderUseNumber = true
89 }
90
91
92
93 func EnableJsonDecoderDisallowUnknownFields() {
94 binding.EnableDecoderDisallowUnknownFields = true
95 }
96
97
98 func Mode() string {
99 return modeName
100 }
101
View as plain text