1
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2
|
|
// Use of this source code is governed by a MIT style
|
3
|
|
// license that can be found in the LICENSE file.
|
4
|
|
|
5
|
|
package gin
|
6
|
|
|
7
|
|
import (
|
8
|
|
"io"
|
9
|
|
"os"
|
10
|
|
|
11
|
|
"github.com/gin-gonic/gin/binding"
|
12
|
|
)
|
13
|
|
|
14
|
|
// EnvGinMode indicates environment name for gin mode.
|
15
|
|
const EnvGinMode = "GIN_MODE"
|
16
|
|
|
17
|
|
const (
|
18
|
|
// DebugMode indicates gin mode is debug.
|
19
|
|
DebugMode = "debug"
|
20
|
|
// ReleaseMode indicates gin mode is release.
|
21
|
|
ReleaseMode = "release"
|
22
|
|
// TestMode indicates gin mode is test.
|
23
|
|
TestMode = "test"
|
24
|
|
)
|
25
|
|
|
26
|
|
const (
|
27
|
|
debugCode = iota
|
28
|
|
releaseCode
|
29
|
|
testCode
|
30
|
|
)
|
31
|
|
|
32
|
|
// DefaultWriter is the default io.Writer used by Gin for debug output and
|
33
|
|
// middleware output like Logger() or Recovery().
|
34
|
|
// Note that both Logger and Recovery provides custom ways to configure their
|
35
|
|
// output io.Writer.
|
36
|
|
// To support coloring in Windows use:
|
37
|
|
// import "github.com/mattn/go-colorable"
|
38
|
|
// gin.DefaultWriter = colorable.NewColorableStdout()
|
39
|
|
var DefaultWriter io.Writer = os.Stdout
|
40
|
|
|
41
|
|
// DefaultErrorWriter is the default io.Writer used by Gin to debug errors
|
42
|
|
var DefaultErrorWriter io.Writer = os.Stderr
|
43
|
|
|
44
|
|
var ginMode = debugCode
|
45
|
|
var modeName = DebugMode
|
46
|
|
|
47
|
|
func init() {
|
48
|
6
|
mode := os.Getenv(EnvGinMode)
|
49
|
6
|
SetMode(mode)
|
50
|
|
}
|
51
|
|
|
52
|
|
// SetMode sets gin mode according to input string.
|
53
|
|
func SetMode(value string) {
|
54
|
6
|
if value == "" {
|
55
|
6
|
value = DebugMode
|
56
|
|
}
|
57
|
|
|
58
|
6
|
switch value {
|
59
|
6
|
case DebugMode:
|
60
|
6
|
ginMode = debugCode
|
61
|
6
|
case ReleaseMode:
|
62
|
6
|
ginMode = releaseCode
|
63
|
6
|
case TestMode:
|
64
|
6
|
ginMode = testCode
|
65
|
6
|
default:
|
66
|
6
|
panic("gin mode unknown: " + value)
|
67
|
|
}
|
68
|
|
|
69
|
6
|
modeName = value
|
70
|
|
}
|
71
|
|
|
72
|
|
// DisableBindValidation closes the default validator.
|
73
|
|
func DisableBindValidation() {
|
74
|
6
|
binding.Validator = nil
|
75
|
|
}
|
76
|
|
|
77
|
|
// EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumber to
|
78
|
|
// call the UseNumber method on the JSON Decoder instance.
|
79
|
|
func EnableJsonDecoderUseNumber() {
|
80
|
6
|
binding.EnableDecoderUseNumber = true
|
81
|
|
}
|
82
|
|
|
83
|
|
// EnableJsonDecoderDisallowUnknownFields sets true for binding.EnableDecoderDisallowUnknownFields to
|
84
|
|
// call the DisallowUnknownFields method on the JSON Decoder instance.
|
85
|
|
func EnableJsonDecoderDisallowUnknownFields() {
|
86
|
6
|
binding.EnableDecoderDisallowUnknownFields = true
|
87
|
|
}
|
88
|
|
|
89
|
|
// Mode returns currently gin mode.
|
90
|
|
func Mode() string {
|
91
|
6
|
return modeName
|
92
|
|
}
|