mode.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // ENV_GIN_MODE indicates environment name for gin mode.
  11. const ENV_GIN_MODE = "GIN_MODE"
  12. const (
  13. // DebugMode indicates gin mode is debug.
  14. DebugMode = "debug"
  15. // ReleaseMode indicates gin mode is relase.
  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 the 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. var DefaultErrorWriter io.Writer = os.Stderr
  34. var ginMode = debugCode
  35. var modeName = DebugMode
  36. func init() {
  37. mode := os.Getenv(ENV_GIN_MODE)
  38. SetMode(mode)
  39. }
  40. // SetMode sets gin mode according to input string.
  41. func SetMode(value string) {
  42. switch value {
  43. case DebugMode, "":
  44. ginMode = debugCode
  45. case ReleaseMode:
  46. ginMode = releaseCode
  47. case TestMode:
  48. ginMode = testCode
  49. default:
  50. panic("gin mode unknown: " + value)
  51. }
  52. if value == "" {
  53. value = DebugMode
  54. }
  55. modeName = value
  56. }
  57. // DisableBindValidation closes the default validator.
  58. func DisableBindValidation() {
  59. binding.Validator = nil
  60. }
  61. // EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumberto to
  62. // call the UseNumber method on the JSON Decoder instance.
  63. func EnableJsonDecoderUseNumber() {
  64. binding.EnableDecoderUseNumber = true
  65. }
  66. // Mode returns currently gin mode.
  67. func Mode() string {
  68. return modeName
  69. }