mode.go 2.2 KB

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