Skip to content

Commit

Permalink
Adds debug mode (part 1)
Browse files Browse the repository at this point in the history
- Adds API to switch the gin's mode
- Log listening port
- Log routes
  • Loading branch information
manucorporat committed Aug 19, 2014
1 parent 312e032 commit 809eee8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gin.go
@@ -1,6 +1,7 @@
package gin

import (
"fmt"
"github.com/gin-gonic/gin/render"
"github.com/julienschmidt/httprouter"
"html/template"
Expand Down Expand Up @@ -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)
}

This comment has been minimized.

Copy link
@kalbasit

kalbasit Aug 24, 2014

I think it's better to have a debugging API, as in:

func Debug(messages ...string) {
  if gin_mode == debugCode {
    fmt.Println("[GIN-debug] ", messages...)
  }
}

func (engine *Engine) Run(add string) {
  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)
}
Expand Down Expand Up @@ -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()
Expand Down
38 changes: 38 additions & 0 deletions 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)
}
}
6 changes: 6 additions & 0 deletions utils.go
Expand Up @@ -2,6 +2,8 @@ package gin

import (
"encoding/xml"
"reflect"
"runtime"
)

type H map[string]interface{}
Expand Down Expand Up @@ -38,3 +40,7 @@ func filterFlags(content string) string {
}
return content
}

func funcName(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}

0 comments on commit 809eee8

Please sign in to comment.