另客网go项目公用的代码库

mode.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. const ENV_GIN_MODE = "GIN_MODE"
  11. const (
  12. DebugMode = "debug"
  13. ReleaseMode = "release"
  14. TestMode = "test"
  15. )
  16. const (
  17. debugCode = iota
  18. releaseCode
  19. testCode
  20. )
  21. // DefaultWriter is the default io.Writer used the Gin for debug output and
  22. // middleware output like Logger() or Recovery().
  23. // Note that both Logger and Recovery provides custom ways to configure their
  24. // output io.Writer.
  25. // To support coloring in Windows use:
  26. // import "github.com/mattn/go-colorable"
  27. // gin.DefaultWriter = colorable.NewColorableStdout()
  28. var DefaultWriter io.Writer = os.Stdout
  29. var DefaultErrorWriter io.Writer = os.Stderr
  30. var ginMode = debugCode
  31. var modeName = DebugMode
  32. func init() {
  33. mode := os.Getenv(ENV_GIN_MODE)
  34. SetMode(mode)
  35. }
  36. func SetMode(value string) {
  37. switch value {
  38. case DebugMode, "":
  39. ginMode = debugCode
  40. case ReleaseMode:
  41. ginMode = releaseCode
  42. case TestMode:
  43. ginMode = testCode
  44. default:
  45. panic("gin mode unknown: " + value)
  46. }
  47. if value == "" {
  48. value = DebugMode
  49. }
  50. modeName = value
  51. }
  52. func DisableBindValidation() {
  53. binding.Validator = nil
  54. }
  55. func EnableJsonDecoderUseNumber() {
  56. binding.EnableDecoderUseNumber = true
  57. }
  58. func Mode() string {
  59. return modeName
  60. }