diff --git a/gin.go b/gin.go index 99c5cbce..c1adbb49 100644 --- a/gin.go +++ b/gin.go @@ -1,6 +1,7 @@ package gin import ( + "fmt" "github.com/gin-gonic/gin/render" "github.com/julienschmidt/httprouter" "html/template" @@ -113,12 +114,18 @@ func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) { } func (engine *Engine) Run(addr string) { + if gin_mode == debugCode { + fmt.Println("[GIN-debug] Listening and serving HTTP on " + addr) + } if err := http.ListenAndServe(addr, engine); err != nil { panic(err) } } func (engine *Engine) RunTLS(addr string, cert string, key string) { + if gin_mode == debugCode { + fmt.Println("[GIN-debug] Listening and serving HTTPS on " + addr) + } if err := http.ListenAndServeTLS(addr, cert, key, engine); err != nil { panic(err) } @@ -168,6 +175,11 @@ func (group *RouterGroup) pathFor(p string) string { func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc) { p = group.pathFor(p) handlers = group.combineHandlers(handlers) + if gin_mode == debugCode { + nuHandlers := len(handlers) + name := funcName(handlers[nuHandlers-1]) + fmt.Printf("[GIN-debug] %-5s %-25s --> %s (%d handlers)\n", method, p, name, nuHandlers) + } group.engine.router.Handle(method, p, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { c := group.engine.createContext(w, req, params, handlers) c.Next() diff --git a/mode.go b/mode.go new file mode 100644 index 00000000..803a2ec4 --- /dev/null +++ b/mode.go @@ -0,0 +1,38 @@ +package gin + +import ( + "os" +) + +const GIN_MODE = "GIN_MODE" + +const ( + DebugMode string = "debug" + ReleaseMode string = "release" +) +const ( + debugCode = iota + releaseCode = iota +) + +var gin_mode int = debugCode + +func SetMode(value string) { + switch value { + case DebugMode: + gin_mode = debugCode + case ReleaseMode: + gin_mode = releaseCode + default: + panic("gin mode unknown, the allowed modes are: " + DebugMode + " and " + ReleaseMode) + } +} + +func init() { + value := os.Getenv(GIN_MODE) + if len(value) == 0 { + SetMode(DebugMode) + } else { + SetMode(value) + } +} diff --git a/utils.go b/utils.go index 90cca1be..6417efd9 100644 --- a/utils.go +++ b/utils.go @@ -2,6 +2,8 @@ package gin import ( "encoding/xml" + "reflect" + "runtime" ) type H map[string]interface{} @@ -38,3 +40,7 @@ func filterFlags(content string) string { } return content } + +func funcName(f interface{}) string { + return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() +}