mode.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 string = "debug"
  13. ReleaseMode string = "release"
  14. TestMode string = "test"
  15. )
  16. const (
  17. debugCode = iota
  18. releaseCode = iota
  19. testCode = iota
  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. if len(mode) == 0 {
  35. SetMode(DebugMode)
  36. } else {
  37. SetMode(mode)
  38. }
  39. }
  40. func SetMode(value string) {
  41. switch value {
  42. case DebugMode:
  43. ginMode = debugCode
  44. case ReleaseMode:
  45. ginMode = releaseCode
  46. case TestMode:
  47. ginMode = testCode
  48. default:
  49. panic("gin mode unknown: " + value)
  50. }
  51. modeName = value
  52. }
  53. func DisableBindValidation() {
  54. binding.Validator = nil
  55. }
  56. func Mode() string {
  57. return modeName
  58. }