gin/mode.go

90 lines
2.2 KiB
Go
Raw Permalink Normal View History

2014-08-29 21:49:50 +04: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 (
2016-04-15 11:12:07 +03:00
"io"
"os"
2015-04-07 19:37:17 +03:00
"github.com/gin-gonic/gin/binding"
)
// EnvGinMode indicates environment name for gin mode.
const EnvGinMode = "GIN_MODE"
const (
// DebugMode indicates gin mode is debug.
DebugMode = "debug"
2018-11-12 13:58:24 +03:00
// ReleaseMode indicates gin mode is release.
ReleaseMode = "release"
// TestMode indicates gin mode is test.
TestMode = "test"
)
const (
2017-03-11 16:35:29 +03:00
debugCode = iota
releaseCode
testCode
)
// DefaultWriter is the default io.Writer used by Gin for debug output and
// 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:
2016-01-27 00:40:29 +03:00
// import "github.com/mattn/go-colorable"
// gin.DefaultWriter = colorable.NewColorableStdout()
2016-04-15 11:12:07 +03:00
var DefaultWriter io.Writer = os.Stdout
// DefaultErrorWriter is the default io.Writer used by Gin to debug errors
2016-04-15 11:12:07 +03:00
var DefaultErrorWriter io.Writer = os.Stderr
2016-04-15 02:16:46 +03:00
var ginMode = debugCode
var modeName = DebugMode
2014-10-09 03:40:42 +04:00
func init() {
mode := os.Getenv(EnvGinMode)
SetMode(mode)
2014-10-09 03:40:42 +04:00
}
// SetMode sets gin mode according to input string.
func SetMode(value string) {
switch value {
case DebugMode, "":
2015-04-07 13:27:23 +03:00
ginMode = debugCode
case ReleaseMode:
2015-04-07 13:27:23 +03:00
ginMode = releaseCode
2014-08-21 03:01:05 +04:00
case TestMode:
2015-04-07 13:27:23 +03:00
ginMode = testCode
default:
2015-04-08 03:58:35 +03:00
panic("gin mode unknown: " + value)
}
if value == "" {
value = DebugMode
}
2015-04-07 13:27:23 +03:00
modeName = value
}
// DisableBindValidation closes the default validator.
func DisableBindValidation() {
binding.Validator = nil
}
// EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumber to
// call the UseNumber method on the JSON Decoder instance.
func EnableJsonDecoderUseNumber() {
binding.EnableDecoderUseNumber = true
}
// EnableJsonDisallowUnknownFields sets true for binding.EnableDecoderDisallowUnknownFields to
// call the DisallowUnknownFields method on the JSON Decoder instance.
func EnableJsonDecoderDisallowUnknownFields() {
binding.EnableDecoderDisallowUnknownFields = true
}
// Mode returns currently gin mode.
func Mode() string {
2015-04-07 13:27:23 +03:00
return modeName
}