gin/mode.go

64 lines
1.0 KiB
Go
Raw Normal View History

2014-08-29 17:49:50 +00:00
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gin
import (
2014-10-08 23:40:42 +00:00
"fmt"
"os"
)
const GIN_MODE = "GIN_MODE"
const (
DebugMode string = "debug"
ReleaseMode string = "release"
2014-08-20 23:01:05 +00:00
TestMode string = "test"
)
const (
debugCode = iota
releaseCode = iota
2014-08-20 23:01:05 +00:00
testCode = iota
)
var gin_mode int = debugCode
var mode_name string = DebugMode
2014-10-08 23:40:42 +00:00
func init() {
value := os.Getenv(GIN_MODE)
if len(value) == 0 {
SetMode(DebugMode)
} else {
SetMode(value)
}
}
func SetMode(value string) {
switch value {
case DebugMode:
gin_mode = debugCode
case ReleaseMode:
gin_mode = releaseCode
2014-08-20 23:01:05 +00:00
case TestMode:
gin_mode = testCode
default:
2014-10-08 23:40:42 +00:00
panic("gin mode unknown: " + value)
}
mode_name = value
}
func Mode() string {
return mode_name
}
2014-10-08 19:37:26 +00:00
func IsDebugging() bool {
return gin_mode == debugCode
}
2014-10-08 23:40:42 +00:00
func debugPrint(format string, values ...interface{}) {
if IsDebugging() {
2014-10-21 14:48:21 +00:00
fmt.Printf("[GIN-debug] "+format, values...)
}
}