mode.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. package gin
  5. import (
  6. "io"
  7. "os"
  8. "github.com/gin-gonic/gin/binding"
  9. )
  10. // EnvGinMode indicates environment name for gin mode.
  11. const EnvGinMode = "GIN_MODE"
  12. const (
  13. // DebugMode indicates gin mode is debug.
  14. DebugMode = "debug"
  15. // ReleaseMode indicates gin mode is release.
  16. ReleaseMode = "release"
  17. // TestMode indicates gin mode is test.
  18. TestMode = "test"
  19. )
  20. const (
  21. debugCode = iota
  22. releaseCode
  23. testCode
  24. )
  25. // DefaultWriter is the default io.Writer used by Gin for debug output and
  26. // middleware output like Logger() or Recovery().
  27. // Note that both Logger and Recovery provides custom ways to configure their
  28. // output io.Writer.
  29. // To support coloring in Windows use:
  30. // import "github.com/mattn/go-colorable"
  31. // gin.DefaultWriter = colorable.NewColorableStdout()
  32. var DefaultWriter io.Writer = os.Stdout
  33. // DefaultErrorWriter is the default io.Writer used by Gin to debug errors
  34. var DefaultErrorWriter io.Writer = os.Stderr
  35. var ginMode = debugCode
  36. var modeName = DebugMode
  37. func init() {
  38. mode := os.Getenv(EnvGinMode)
  39. SetMode(mode)
  40. }
  41. // SetMode sets gin mode according to input string.
  42. func SetMode(value string) {
  43. switch value {
  44. case DebugMode, "":
  45. ginMode = debugCode
  46. case ReleaseMode:
  47. ginMode = releaseCode
  48. case TestMode:
  49. ginMode = testCode
  50. default:
  51. panic("gin mode unknown: " + value)
  52. }
  53. if value == "" {
  54. value = DebugMode
  55. }
  56. modeName = value
  57. }
  58. // DisableBindValidation closes the default validator.
  59. func DisableBindValidation() {
  60. binding.Validator = nil
  61. }
  62. // EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumber to
  63. // call the UseNumber method on the JSON Decoder instance.
  64. func EnableJsonDecoderUseNumber() {
  65. binding.EnableDecoderUseNumber = true
  66. }
  67. // EnableJsonDisallowUnknownFields sets true for binding.EnableDecoderDisallowUnknownFields to
  68. // call the DisallowUnknownFields method on the JSON Decoder instance.
  69. func EnableJsonDecoderDisallowUnknownFields() {
  70. binding.EnableDecoderDisallowUnknownFields = true
  71. }
  72. // Mode returns currently gin mode.
  73. func Mode() string {
  74. return modeName
  75. }