mirror of https://github.com/gin-gonic/gin.git
Adds debug mode (part 1)
- Adds API to switch the gin's mode - Log listening port - Log routes
This commit is contained in:
parent
312e032960
commit
809eee8a72
12
gin.go
12
gin.go
|
@ -1,6 +1,7 @@
|
||||||
package gin
|
package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/gin-gonic/gin/render"
|
"github.com/gin-gonic/gin/render"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
@ -113,12 +114,18 @@ func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) Run(addr string) {
|
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 {
|
if err := http.ListenAndServe(addr, engine); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) RunTLS(addr string, cert string, key string) {
|
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 {
|
if err := http.ListenAndServeTLS(addr, cert, key, engine); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -168,6 +175,11 @@ func (group *RouterGroup) pathFor(p string) string {
|
||||||
func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc) {
|
func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc) {
|
||||||
p = group.pathFor(p)
|
p = group.pathFor(p)
|
||||||
handlers = group.combineHandlers(handlers)
|
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) {
|
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 := group.engine.createContext(w, req, params, handlers)
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
6
utils.go
6
utils.go
|
@ -2,6 +2,8 @@ package gin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"reflect"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
type H map[string]interface{}
|
type H map[string]interface{}
|
||||||
|
@ -38,3 +40,7 @@ func filterFlags(content string) string {
|
||||||
}
|
}
|
||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func funcName(f interface{}) string {
|
||||||
|
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue