...

Source file src/github.com/gin-gonic/gin/mode.go

Documentation: github.com/gin-gonic/gin

     1  // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
     2  // Use of this source code is governed by a MIT style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gin
     6  
     7  import (
     8  	"flag"
     9  	"io"
    10  	"os"
    11  
    12  	"github.com/gin-gonic/gin/binding"
    13  )
    14  
    15  // EnvGinMode indicates environment name for gin mode.
    16  const EnvGinMode = "GIN_MODE"
    17  
    18  const (
    19  	// DebugMode indicates gin mode is debug.
    20  	DebugMode = "debug"
    21  	// ReleaseMode indicates gin mode is release.
    22  	ReleaseMode = "release"
    23  	// TestMode indicates gin mode is test.
    24  	TestMode = "test"
    25  )
    26  
    27  const (
    28  	debugCode = iota
    29  	releaseCode
    30  	testCode
    31  )
    32  
    33  // DefaultWriter is the default io.Writer used by Gin for debug output and
    34  // middleware output like Logger() or Recovery().
    35  // Note that both Logger and Recovery provides custom ways to configure their
    36  // output io.Writer.
    37  // To support coloring in Windows use:
    38  //
    39  //	import "github.com/mattn/go-colorable"
    40  //	gin.DefaultWriter = colorable.NewColorableStdout()
    41  var DefaultWriter io.Writer = os.Stdout
    42  
    43  // DefaultErrorWriter is the default io.Writer used by Gin to debug errors
    44  var DefaultErrorWriter io.Writer = os.Stderr
    45  
    46  var (
    47  	ginMode  = debugCode
    48  	modeName = DebugMode
    49  )
    50  
    51  func init() {
    52  	mode := os.Getenv(EnvGinMode)
    53  	SetMode(mode)
    54  }
    55  
    56  // SetMode sets gin mode according to input string.
    57  func SetMode(value string) {
    58  	if value == "" {
    59  		if flag.Lookup("test.v") != nil {
    60  			value = TestMode
    61  		} else {
    62  			value = DebugMode
    63  		}
    64  	}
    65  
    66  	switch value {
    67  	case DebugMode:
    68  		ginMode = debugCode
    69  	case ReleaseMode:
    70  		ginMode = releaseCode
    71  	case TestMode:
    72  		ginMode = testCode
    73  	default:
    74  		panic("gin mode unknown: " + value + " (available mode: debug release test)")
    75  	}
    76  
    77  	modeName = value
    78  }
    79  
    80  // DisableBindValidation closes the default validator.
    81  func DisableBindValidation() {
    82  	binding.Validator = nil
    83  }
    84  
    85  // EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumber to
    86  // call the UseNumber method on the JSON Decoder instance.
    87  func EnableJsonDecoderUseNumber() {
    88  	binding.EnableDecoderUseNumber = true
    89  }
    90  
    91  // EnableJsonDecoderDisallowUnknownFields sets true for binding.EnableDecoderDisallowUnknownFields to
    92  // call the DisallowUnknownFields method on the JSON Decoder instance.
    93  func EnableJsonDecoderDisallowUnknownFields() {
    94  	binding.EnableDecoderDisallowUnknownFields = true
    95  }
    96  
    97  // Mode returns current gin mode.
    98  func Mode() string {
    99  	return modeName
   100  }
   101  

View as plain text