mode.go 875 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. "log"
  7. "os"
  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 gin_mode int = debugCode
  21. var mode_name string = DebugMode
  22. func init() {
  23. value := os.Getenv(GIN_MODE)
  24. if len(value) == 0 {
  25. SetMode(DebugMode)
  26. } else {
  27. SetMode(value)
  28. }
  29. }
  30. func SetMode(value string) {
  31. switch value {
  32. case DebugMode:
  33. gin_mode = debugCode
  34. case ReleaseMode:
  35. gin_mode = releaseCode
  36. case TestMode:
  37. gin_mode = testCode
  38. default:
  39. log.Panic("gin mode unknown: " + value)
  40. }
  41. mode_name = value
  42. }
  43. func Mode() string {
  44. return mode_name
  45. }