gin-gonic / gin
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 16
	mode := os.Getenv(EnvGinMode)
49 16
	SetMode(mode)
50
}
51

52
// SetMode sets gin mode according to input string.
53
func SetMode(value string) {
54 16
	if value == "" {
55 16
		value = DebugMode
56
	}
57

58 16
	switch value {
59 16
	case DebugMode:
60 16
		ginMode = debugCode
61 16
	case ReleaseMode:
62 16
		ginMode = releaseCode
63 16
	case TestMode:
64 16
		ginMode = testCode
65 16
	default:
66 16
		panic("gin mode unknown: " + value + " (available mode: debug release test)")
67
	}
68

69 16
	modeName = value
70
}
71

72
// DisableBindValidation closes the default validator.
73
func DisableBindValidation() {
74 16
	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 16
	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 16
	binding.EnableDecoderDisallowUnknownFields = true
87
}
88

89
// Mode returns currently gin mode.
90
func Mode() string {
91 16
	return modeName
92
}

Read our documentation on viewing source code .

Loading