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