2022-05-28 02:42:28 +00:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2015-03-23 05:03:12 +00:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gin
|
|
|
|
|
2015-08-03 15:28:12 +00:00
|
|
|
import (
|
2018-09-19 05:57:00 +00:00
|
|
|
"fmt"
|
2015-08-03 15:28:12 +00:00
|
|
|
"html/template"
|
2018-09-26 05:49:11 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-08-03 15:28:12 +00:00
|
|
|
)
|
2015-03-23 05:03:12 +00:00
|
|
|
|
2023-03-01 02:03:48 +00:00
|
|
|
const ginSupportMinGoVer = 18
|
2018-09-26 05:49:11 +00:00
|
|
|
|
2015-07-02 18:24:54 +00:00
|
|
|
// IsDebugging returns true if the framework is running in debug mode.
|
2017-11-02 13:48:54 +00:00
|
|
|
// Use SetMode(gin.ReleaseMode) to disable debug mode.
|
2015-03-23 05:03:12 +00:00
|
|
|
func IsDebugging() bool {
|
2015-04-07 10:27:23 +00:00
|
|
|
return ginMode == debugCode
|
2015-03-23 05:03:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-20 03:53:58 +00:00
|
|
|
// DebugPrintRouteFunc indicates debug log output format.
|
2018-09-17 04:09:34 +00:00
|
|
|
var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)
|
|
|
|
|
2024-03-11 14:41:07 +00:00
|
|
|
// DebugPrintFunc indicates debug log output format.
|
|
|
|
var DebugPrintFunc func(format string, values ...interface{})
|
|
|
|
|
2015-05-09 01:34:43 +00:00
|
|
|
func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
|
2015-03-23 05:03:12 +00:00
|
|
|
if IsDebugging() {
|
|
|
|
nuHandlers := len(handlers)
|
2015-06-07 11:49:36 +00:00
|
|
|
handlerName := nameOfFunction(handlers.Last())
|
2018-09-17 04:09:34 +00:00
|
|
|
if DebugPrintRouteFunc == nil {
|
|
|
|
debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)
|
|
|
|
} else {
|
|
|
|
DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)
|
|
|
|
}
|
2015-03-23 05:03:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 15:28:12 +00:00
|
|
|
func debugPrintLoadTemplate(tmpl *template.Template) {
|
|
|
|
if IsDebugging() {
|
2019-06-29 12:43:32 +00:00
|
|
|
var buf strings.Builder
|
2015-08-03 15:28:12 +00:00
|
|
|
for _, tmpl := range tmpl.Templates() {
|
|
|
|
buf.WriteString("\t- ")
|
|
|
|
buf.WriteString(tmpl.Name())
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
|
|
|
debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 01:43:17 +00:00
|
|
|
func debugPrint(format string, values ...any) {
|
2024-03-11 14:41:07 +00:00
|
|
|
if !IsDebugging() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if DebugPrintFunc != nil {
|
|
|
|
DebugPrintFunc(format, values...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(format, "\n") {
|
|
|
|
format += "\n"
|
2015-03-23 05:03:12 +00:00
|
|
|
}
|
2024-03-11 14:41:07 +00:00
|
|
|
fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)
|
2015-03-23 05:03:12 +00:00
|
|
|
}
|
2015-05-09 01:34:43 +00:00
|
|
|
|
2018-09-26 05:49:11 +00:00
|
|
|
func getMinVer(v string) (uint64, error) {
|
|
|
|
first := strings.IndexByte(v, '.')
|
|
|
|
last := strings.LastIndexByte(v, '.')
|
|
|
|
if first == last {
|
|
|
|
return strconv.ParseUint(v[first+1:], 10, 64)
|
|
|
|
}
|
|
|
|
return strconv.ParseUint(v[first+1:last], 10, 64)
|
|
|
|
}
|
|
|
|
|
2017-09-29 03:58:57 +00:00
|
|
|
func debugPrintWARNINGDefault() {
|
2022-07-01 02:31:31 +00:00
|
|
|
if v, e := getMinVer(runtime.Version()); e == nil && v < ginSupportMinGoVer {
|
2023-03-01 02:03:48 +00:00
|
|
|
debugPrint(`[WARNING] Now Gin requires Go 1.18+.
|
2018-06-26 10:56:43 +00:00
|
|
|
|
|
|
|
`)
|
2018-09-26 05:49:11 +00:00
|
|
|
}
|
2017-09-29 03:58:57 +00:00
|
|
|
debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
|
|
|
|
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2015-07-02 18:24:54 +00:00
|
|
|
func debugPrintWARNINGNew() {
|
2015-05-23 23:21:23 +00:00
|
|
|
debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.
|
|
|
|
- using env: export GIN_MODE=release
|
|
|
|
- using code: gin.SetMode(gin.ReleaseMode)
|
|
|
|
|
|
|
|
`)
|
2015-05-09 01:34:43 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 18:24:54 +00:00
|
|
|
func debugPrintWARNINGSetHTMLTemplate() {
|
2015-06-26 14:08:55 +00:00
|
|
|
debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called
|
|
|
|
at initialization. ie. before any route is registered or the router is listening in a socket:
|
|
|
|
|
|
|
|
router := gin.Default()
|
|
|
|
router.SetHTMLTemplate(template) // << good place
|
|
|
|
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2015-05-09 01:34:43 +00:00
|
|
|
func debugPrintError(err error) {
|
2021-06-03 12:12:51 +00:00
|
|
|
if err != nil && IsDebugging() {
|
|
|
|
fmt.Fprintf(DefaultErrorWriter, "[GIN-debug] [ERROR] %v\n", err)
|
2015-05-09 01:34:43 +00:00
|
|
|
}
|
|
|
|
}
|