2019-01-28 03:19:49 +00:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
)
|
|
|
|
|
2019-01-31 08:57:22 +00:00
|
|
|
// ENV_GIN_MODE indicates environment name for gin mode.
|
2019-01-28 03:19:49 +00:00
|
|
|
const ENV_GIN_MODE = "GIN_MODE"
|
|
|
|
|
|
|
|
const (
|
2019-01-31 08:57:22 +00:00
|
|
|
// DebugMode indicates gin mode is debug.
|
|
|
|
DebugMode = "debug"
|
|
|
|
// ReleaseMode indicates gin mode is release.
|
2019-01-28 03:19:49 +00:00
|
|
|
ReleaseMode = "release"
|
2019-01-31 08:57:22 +00:00
|
|
|
// TestMode indicates gin mode is test.
|
|
|
|
TestMode = "test"
|
2019-01-28 03:19:49 +00:00
|
|
|
)
|
|
|
|
const (
|
|
|
|
debugCode = iota
|
|
|
|
releaseCode
|
|
|
|
testCode
|
|
|
|
)
|
|
|
|
|
2019-01-31 08:57:22 +00:00
|
|
|
// DefaultWriter is the default io.Writer used by Gin for debug output and
|
2019-01-28 03:19:49 +00:00
|
|
|
// middleware output like Logger() or Recovery().
|
|
|
|
// Note that both Logger and Recovery provides custom ways to configure their
|
|
|
|
// output io.Writer.
|
|
|
|
// To support coloring in Windows use:
|
|
|
|
// import "github.com/mattn/go-colorable"
|
|
|
|
// gin.DefaultWriter = colorable.NewColorableStdout()
|
|
|
|
var DefaultWriter io.Writer = os.Stdout
|
2019-01-31 08:57:22 +00:00
|
|
|
|
|
|
|
// DefaultErrorWriter is the default io.Writer used by Gin to debug errors
|
2019-01-28 03:19:49 +00:00
|
|
|
var DefaultErrorWriter io.Writer = os.Stderr
|
|
|
|
|
|
|
|
var ginMode = debugCode
|
|
|
|
var modeName = DebugMode
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
mode := os.Getenv(ENV_GIN_MODE)
|
|
|
|
SetMode(mode)
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:57:22 +00:00
|
|
|
// SetMode sets gin mode according to input string.
|
2019-01-28 03:19:49 +00:00
|
|
|
func SetMode(value string) {
|
|
|
|
switch value {
|
|
|
|
case DebugMode, "":
|
|
|
|
ginMode = debugCode
|
|
|
|
case ReleaseMode:
|
|
|
|
ginMode = releaseCode
|
|
|
|
case TestMode:
|
|
|
|
ginMode = testCode
|
|
|
|
default:
|
|
|
|
panic("gin mode unknown: " + value)
|
|
|
|
}
|
|
|
|
if value == "" {
|
|
|
|
value = DebugMode
|
|
|
|
}
|
|
|
|
modeName = value
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:57:22 +00:00
|
|
|
// DisableBindValidation closes the default validator.
|
2019-01-28 03:19:49 +00:00
|
|
|
func DisableBindValidation() {
|
|
|
|
binding.Validator = nil
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:57:22 +00:00
|
|
|
// EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumberto to
|
|
|
|
// call the UseNumber method on the JSON Decoder instance.
|
2019-01-28 03:19:49 +00:00
|
|
|
func EnableJsonDecoderUseNumber() {
|
|
|
|
binding.EnableDecoderUseNumber = true
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:57:22 +00:00
|
|
|
// Mode returns currently gin mode.
|
2019-01-28 03:19:49 +00:00
|
|
|
func Mode() string {
|
|
|
|
return modeName
|
|
|
|
}
|