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 5package gin 6 7import ( 8 "io" 9 "os" 10 11 "github.com/gin-gonic/gin/binding" 12) 13 14// EnvGinMode indicates environment name for gin mode. 15const EnvGinMode = "GIN_MODE" 16 17const ( 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 26const ( 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() 39var DefaultWriter io.Writer = os.Stdout 40 41// DefaultErrorWriter is the default io.Writer used by Gin to debug errors 42var DefaultErrorWriter io.Writer = os.Stderr 43 44var ginMode = debugCode 45var modeName = DebugMode 46 47func init() { 48 mode := os.Getenv(EnvGinMode) 49 SetMode(mode) 50} 51 52// SetMode sets gin mode according to input string. 53func SetMode(value string) { 54 if value == "" { 55 value = DebugMode 56 } 57 58 switch value { 59 case DebugMode: 60 ginMode = debugCode 61 case ReleaseMode: 62 ginMode = releaseCode 63 case TestMode: 64 ginMode = testCode 65 default: 66 panic("gin mode unknown: " + value + " (available mode: debug release test)") 67 } 68 69 modeName = value 70} 71 72// DisableBindValidation closes the default validator. 73func DisableBindValidation() { 74 binding.Validator = nil 75} 76 77// EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumber to 78// call the UseNumber method on the JSON Decoder instance. 79func EnableJsonDecoderUseNumber() { 80 binding.EnableDecoderUseNumber = true 81} 82 83// EnableJsonDecoderDisallowUnknownFields sets true for binding.EnableDecoderDisallowUnknownFields to 84// call the DisallowUnknownFields method on the JSON Decoder instance. 85func EnableJsonDecoderDisallowUnknownFields() { 86 binding.EnableDecoderDisallowUnknownFields = true 87} 88 89// Mode returns currently gin mode. 90func Mode() string { 91 return modeName 92} 93