Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com> Co-authored-by: thinkerou <thinkerou@gmail.com>
1 |
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
2 |
// Use of this source code is governed by a MIT style
|
|
3 |
// license that can be found in the LICENSE file.
|
|
4 |
|
|
5 |
package gin |
|
6 |
|
|
7 |
import ( |
|
8 |
"fmt"
|
|
9 |
"html/template"
|
|
10 |
"runtime"
|
|
11 |
"strconv"
|
|
12 |
"strings"
|
|
13 |
)
|
|
14 |
|
|
15 |
const ginSupportMinGoVer = 13 |
|
16 |
|
|
17 |
// IsDebugging returns true if the framework is running in debug mode.
|
|
18 |
// Use SetMode(gin.ReleaseMode) to disable debug mode.
|
|
19 |
func IsDebugging() bool { |
|
20 | 18 |
return ginMode == debugCode |
21 |
}
|
|
22 |
|
|
23 |
// DebugPrintRouteFunc indicates debug log output format.
|
|
24 |
var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int) |
|
25 |
|
|
26 |
func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) { |
|
27 |
if IsDebugging() { |
|
28 | 18 |
nuHandlers := len(handlers) |
29 | 18 |
handlerName := nameOfFunction(handlers.Last()) |
30 |
if DebugPrintRouteFunc == nil { |
|
31 | 18 |
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers) |
32 |
} else { |
|
33 | 18 |
DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers) |
34 |
}
|
|
35 |
}
|
|
36 |
}
|
|
37 |
|
|
38 |
func debugPrintLoadTemplate(tmpl *template.Template) { |
|
39 | 18 |
if IsDebugging() { |
40 | 18 |
var buf strings.Builder |
41 |
for _, tmpl := range tmpl.Templates() { |
|
42 | 18 |
buf.WriteString("\t- ") |
43 | 18 |
buf.WriteString(tmpl.Name()) |
44 | 18 |
buf.WriteString("\n") |
45 |
}
|
|
46 | 18 |
debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String()) |
47 |
}
|
|
48 |
}
|
|
49 |
|
|
50 |
func debugPrint(format string, values ...interface{}) { |
|
51 |
if IsDebugging() { |
|
52 |
if !strings.HasSuffix(format, "\n") { |
|
53 | 18 |
format += "\n" |
54 |
}
|
|
55 | 18 |
fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...) |
56 |
}
|
|
57 |
}
|
|
58 |
|
|
59 |
func getMinVer(v string) (uint64, error) { |
|
60 | 18 |
first := strings.IndexByte(v, '.') |
61 | 18 |
last := strings.LastIndexByte(v, '.') |
62 |
if first == last { |
|
63 | 18 |
return strconv.ParseUint(v[first+1:], 10, 64) |
64 |
}
|
|
65 | 18 |
return strconv.ParseUint(v[first+1:last], 10, 64) |
66 |
}
|
|
67 |
|
|
68 |
func debugPrintWARNINGDefault() { |
|
69 |
if v, e := getMinVer(runtime.Version()); e == nil && v <= ginSupportMinGoVer { |
|
70 | 4 |
debugPrint(`[WARNING] Now Gin requires Go 1.13+. |
71 |
|
|
72 | 4 |
`) |
73 |
}
|
|
74 | 18 |
debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached. |
75 |
|
|
76 | 18 |
`) |
77 |
}
|
|
78 |
|
|
79 |
func debugPrintWARNINGNew() { |
|
80 | 18 |
debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production. |
81 | 18 |
- using env: export GIN_MODE=release
|
82 | 18 |
- using code: gin.SetMode(gin.ReleaseMode)
|
83 |
|
|
84 | 18 |
`) |
85 |
}
|
|
86 |
|
|
87 |
func debugPrintWARNINGSetHTMLTemplate() { |
|
88 | 18 |
debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called |
89 | 18 |
at initialization. ie. before any route is registered or the router is listening in a socket:
|
90 |
|
|
91 | 18 |
router := gin.Default()
|
92 | 18 |
router.SetHTMLTemplate(template) // << good place
|
93 |
|
|
94 | 18 |
`) |
95 |
}
|
|
96 |
|
|
97 |
func debugPrintError(err error) { |
|
98 |
if err != nil { |
|
99 |
if IsDebugging() { |
|
100 | 18 |
fmt.Fprintf(DefaultErrorWriter, "[GIN-debug] [ERROR] %v\n", err) |
101 |
}
|
|
102 |
}
|
|
103 |
}
|
Read our documentation on viewing source code .