mode.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. "github.com/mattn/go-colorable"
  10. )
  11. const ENV_GIN_MODE = "GIN_MODE"
  12. const (
  13. DebugMode string = "debug"
  14. ReleaseMode string = "release"
  15. TestMode string = "test"
  16. )
  17. const (
  18. debugCode = iota
  19. releaseCode = iota
  20. testCode = iota
  21. )
  22. var DefaultWriter io.Writer = colorable.NewColorableStdout()
  23. var ginMode int = debugCode
  24. var modeName string = DebugMode
  25. func init() {
  26. mode := os.Getenv(ENV_GIN_MODE)
  27. if len(mode) == 0 {
  28. SetMode(DebugMode)
  29. } else {
  30. SetMode(mode)
  31. }
  32. }
  33. func SetMode(value string) {
  34. switch value {
  35. case DebugMode:
  36. ginMode = debugCode
  37. case ReleaseMode:
  38. ginMode = releaseCode
  39. case TestMode:
  40. ginMode = testCode
  41. default:
  42. panic("gin mode unknown: " + value)
  43. }
  44. modeName = value
  45. }
  46. func DisableBindValidation() {
  47. binding.Validator = nil
  48. }
  49. func Mode() string {
  50. return modeName
  51. }