mode.go 1.3 KB

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