forked from mirror/gin
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
|
||||
|
||||
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()
|
||||
|
|
|
@ -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 (
|
||||
"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()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue