mode.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "fmt"
  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. panic("gin mode unknown: " + value)
  40. }
  41. mode_name = value
  42. }
  43. func Mode() string {
  44. return mode_name
  45. }
  46. func IsDebugging() bool {
  47. return gin_mode == debugCode
  48. }
  49. func debugPrint(format string, values ...interface{}) {
  50. if IsDebugging() {
  51. fmt.Printf("[GIN-debug] "+format, values...)
  52. }
  53. }