mode.go 963 B

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