gin/mode.go

70 lines
1.4 KiB
Go
Raw Normal View History

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.
package gin
import (
2016-04-15 14:12:07 +06:00
"io"
"os"
2015-04-07 18:37:17 +02:00
"github.com/gin-gonic/gin/binding"
)
2015-05-22 16:55:16 +02:00
const ENV_GIN_MODE = "GIN_MODE"
const (
DebugMode string = "debug"
ReleaseMode string = "release"
2014-08-21 01:01:05 +02:00
TestMode string = "test"
)
const (
2017-03-11 07:35:29 -06:00
debugCode = iota
releaseCode
testCode
)
// 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 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
var DefaultErrorWriter io.Writer = os.Stderr
2016-04-15 01:16:46 +02:00
var ginMode = debugCode
var modeName = DebugMode
2014-10-09 01:40:42 +02:00
func init() {
2015-05-22 16:55:16 +02:00
mode := os.Getenv(ENV_GIN_MODE)
if len(mode) == 0 {
2014-10-09 01:40:42 +02:00
SetMode(DebugMode)
} else {
2015-05-22 16:55:16 +02:00
SetMode(mode)
2014-10-09 01:40:42 +02:00
}
}
func SetMode(value string) {
switch value {
case DebugMode:
2015-04-07 12:27:23 +02:00
ginMode = debugCode
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
default:
2015-04-08 02:58:35 +02:00
panic("gin mode unknown: " + value)
}
2015-04-07 12:27:23 +02:00
modeName = value
}
func DisableBindValidation() {
binding.Validator = nil
}
func Mode() string {
2015-04-07 12:27:23 +02:00
return modeName
}