mode.go 942 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/mattn/go-colorable"
  8. )
  9. const 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. var DefaultWriter = colorable.NewColorableStdout()
  21. var ginMode int = debugCode
  22. var modeName string = DebugMode
  23. func init() {
  24. value := os.Getenv(GIN_MODE)
  25. if len(value) == 0 {
  26. SetMode(DebugMode)
  27. } else {
  28. SetMode(value)
  29. }
  30. }
  31. func SetMode(value string) {
  32. switch value {
  33. case DebugMode:
  34. ginMode = debugCode
  35. case ReleaseMode:
  36. ginMode = releaseCode
  37. case TestMode:
  38. ginMode = testCode
  39. default:
  40. panic("gin mode unknown: " + value)
  41. }
  42. modeName = value
  43. }
  44. func Mode() string {
  45. return modeName
  46. }