mode.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. // ```
  27. // import "github.com/mattn/go-colorable"
  28. // gin.DefaultWriter = colorable.NewColorableStdout()
  29. // ```
  30. var DefaultWriter io.Writer = os.Stdout
  31. var DefaultErrorWriter io.Writer = os.Stderr
  32. var ginMode int = debugCode
  33. var modeName string = DebugMode
  34. func init() {
  35. mode := os.Getenv(ENV_GIN_MODE)
  36. if len(mode) == 0 {
  37. SetMode(DebugMode)
  38. } else {
  39. SetMode(mode)
  40. }
  41. }
  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. modeName = value
  54. }
  55. func DisableBindValidation() {
  56. binding.Validator = nil
  57. }
  58. func Mode() string {
  59. return modeName
  60. }