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