mode.go 821 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. func SetMode(value string) {
  21. switch value {
  22. case DebugMode:
  23. gin_mode = debugCode
  24. case ReleaseMode:
  25. gin_mode = releaseCode
  26. case TestMode:
  27. gin_mode = testCode
  28. default:
  29. panic("gin mode unknown, the allowed modes are: " + DebugMode + " and " + ReleaseMode)
  30. }
  31. }
  32. func init() {
  33. value := os.Getenv(GIN_MODE)
  34. if len(value) == 0 {
  35. SetMode(DebugMode)
  36. } else {
  37. SetMode(value)
  38. }
  39. }