mode.go
This file
Path
mode.go
100 lines · 2.4 KiB · Go
Last changed by
chore(mode): remove impossible case (empty value for mode) (#4303)maskppbefore adoption57ec9e6
History of this fileTake it
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package gin67import (8 "flag"9 "io"10 "os"11 "sync/atomic"1213 "github.com/gin-gonic/gin/binding"14)1516// EnvGinMode indicates environment name for gin mode.17const EnvGinMode = "GIN_MODE"1819const (20 // DebugMode indicates gin mode is debug.21 DebugMode = "debug"22 // ReleaseMode indicates gin mode is release.23 ReleaseMode = "release"24 // TestMode indicates gin mode is test.25 TestMode = "test"26)2728const (29 debugCode = iota30 releaseCode31 testCode32)3334// DefaultWriter is the default io.Writer used by Gin for debug output and35// middleware output like Logger() or Recovery().36// Note that both Logger and Recovery provides custom ways to configure their37// output io.Writer.38// To support coloring in Windows use:39//40// import "github.com/mattn/go-colorable"41// gin.DefaultWriter = colorable.NewColorableStdout()42var DefaultWriter io.Writer = os.Stdout4344// DefaultErrorWriter is the default io.Writer used by Gin to debug errors45var DefaultErrorWriter io.Writer = os.Stderr4647var (48 ginMode int32 = debugCode49 modeName atomic.Value50)5152func init() {53 mode := os.Getenv(EnvGinMode)54 SetMode(mode)55}5657// SetMode sets gin mode according to input string.58func SetMode(value string) {59 if value == "" {60 if flag.Lookup("test.v") != nil {61 value = TestMode62 } else {63 value = DebugMode64 }65 }6667 switch value {68 case DebugMode:69 atomic.StoreInt32(&ginMode, debugCode)70 case ReleaseMode:71 atomic.StoreInt32(&ginMode, releaseCode)72 case TestMode:73 atomic.StoreInt32(&ginMode, testCode)74 default:75 panic("gin mode unknown: " + value + " (available mode: debug release test)")76 }77 modeName.Store(value)78}7980// DisableBindValidation closes the default validator.81func DisableBindValidation() {82 binding.Validator = nil83}8485// EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumber to86// call the UseNumber method on the JSON Decoder instance.87func EnableJsonDecoderUseNumber() {88 binding.EnableDecoderUseNumber = true89}9091// EnableJsonDecoderDisallowUnknownFields sets true for binding.EnableDecoderDisallowUnknownFields to92// call the DisallowUnknownFields method on the JSON Decoder instance.93func EnableJsonDecoderDisallowUnknownFields() {94 binding.EnableDecoderDisallowUnknownFields = true95}9697// Mode returns current gin mode.98func Mode() string {99 return modeName.Load().(string)100}