From 48fec0650dd70e158755b5d2fd839832dbae4437 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Mon, 23 Mar 2015 06:03:12 +0100 Subject: [PATCH] Cosmetic changes --- auth_test.go | 2 +- context.go | 2 +- debug.go | 25 +++++++++++++++++++++++++ deprecated.go | 3 ++- logger.go | 3 ++- mode.go | 10 ---------- routergroup.go | 9 +++------ 7 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 debug.go diff --git a/auth_test.go b/auth_test.go index 1ea1d50b..d2f165cd 100644 --- a/auth_test.go +++ b/auth_test.go @@ -76,7 +76,7 @@ func TestBasicAuth401WithCustomRealm(t *testing.T) { r.ServeHTTP(w, req) if w.Code != 401 { - t.Errorf("Response code should be Not autorized, was: %s", w.Code) + t.Errorf("Response code should be Not autorized, was: %d", w.Code) } if w.HeaderMap.Get("WWW-Authenticate") != "Basic realm=\"My Custom Realm\"" { diff --git a/context.go b/context.go index d9a7ab1a..85caf996 100644 --- a/context.go +++ b/context.go @@ -362,7 +362,7 @@ func (c *Context) Redirect(code int, location string) { if code >= 300 && code <= 308 { c.Render(code, render.Redirect, location) } else { - panic(fmt.Sprintf("Cannot send a redirect with status code %d", code)) + log.Panicf("Cannot send a redirect with status code %d", code) } } diff --git a/debug.go b/debug.go new file mode 100644 index 00000000..cfac22c2 --- /dev/null +++ b/debug.go @@ -0,0 +1,25 @@ +// 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 "log" + +func IsDebugging() bool { + return gin_mode == debugCode +} + +func debugRoute(httpMethod, absolutePath string, handlers []HandlerFunc) { + if IsDebugging() { + nuHandlers := len(handlers) + handlerName := nameOfFunction(handlers[nuHandlers-1]) + debugPrint("%-5s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers) + } +} + +func debugPrint(format string, values ...interface{}) { + if IsDebugging() { + log.Printf("[GIN-debug] "+format, values...) + } +} diff --git a/deprecated.go b/deprecated.go index 71881530..2f53c08d 100644 --- a/deprecated.go +++ b/deprecated.go @@ -5,8 +5,9 @@ package gin import ( - "github.com/gin-gonic/gin/binding" "net/http" + + "github.com/gin-gonic/gin/binding" ) // DEPRECATED, use Bind() instead. diff --git a/logger.go b/logger.go index 0f1f34b1..478953aa 100644 --- a/logger.go +++ b/logger.go @@ -5,9 +5,10 @@ package gin import ( - "github.com/mattn/go-colorable" "log" "time" + + "github.com/mattn/go-colorable" ) var ( diff --git a/mode.go b/mode.go index 59c8d506..c9ff0327 100644 --- a/mode.go +++ b/mode.go @@ -51,13 +51,3 @@ func SetMode(value string) { func Mode() string { return mode_name } - -func IsDebugging() bool { - return gin_mode == debugCode -} - -func debugPrint(format string, values ...interface{}) { - if IsDebugging() { - log.Printf("[GIN-debug] "+format, values...) - } -} diff --git a/routergroup.go b/routergroup.go index 8e02a402..c70bb34e 100644 --- a/routergroup.go +++ b/routergroup.go @@ -5,9 +5,10 @@ package gin import ( - "github.com/julienschmidt/httprouter" "net/http" "path" + + "github.com/julienschmidt/httprouter" ) // Used internally to configure router, a RouterGroup is associated with a prefix @@ -46,11 +47,7 @@ func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *R func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers []HandlerFunc) { absolutePath := group.calculateAbsolutePath(relativePath) handlers = group.combineHandlers(handlers) - if IsDebugging() { - nuHandlers := len(handlers) - handlerName := nameOfFunction(handlers[nuHandlers-1]) - debugPrint("%-5s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers) - } + debugRoute(httpMethod, absolutePath, handlers) group.engine.router.Handle(httpMethod, absolutePath, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { context := group.engine.createContext(w, req, params, handlers)