mode.go 915 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. )
  8. const GIN_MODE = "GIN_MODE"
  9. const (
  10. DebugMode string = "debug"
  11. ReleaseMode string = "release"
  12. TestMode string = "test"
  13. )
  14. const (
  15. debugCode = iota
  16. releaseCode = iota
  17. testCode = iota
  18. )
  19. var gin_mode int = debugCode
  20. var mode_name string = DebugMode
  21. func SetMode(value string) {
  22. switch value {
  23. case DebugMode:
  24. gin_mode = debugCode
  25. case ReleaseMode:
  26. gin_mode = releaseCode
  27. case TestMode:
  28. gin_mode = testCode
  29. default:
  30. panic("gin mode unknown, the allowed modes are: " + DebugMode + " and " + ReleaseMode)
  31. }
  32. mode_name = value
  33. }
  34. func Mode() string {
  35. return mode_name
  36. }
  37. func init() {
  38. value := os.Getenv(GIN_MODE)
  39. if len(value) == 0 {
  40. SetMode(DebugMode)
  41. } else {
  42. SetMode(value)
  43. }
  44. }