podman
101 строка · 2.7 Кб
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
5package gin6
7import (8"fmt"9"html/template"10"runtime"11"strconv"12"strings"13)
14
15const ginSupportMinGoVer = 1816
17// IsDebugging returns true if the framework is running in debug mode.
18// Use SetMode(gin.ReleaseMode) to disable debug mode.
19func IsDebugging() bool {20return ginMode == debugCode21}
22
23// DebugPrintRouteFunc indicates debug log output format.
24var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)25
26func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {27if IsDebugging() {28nuHandlers := len(handlers)29handlerName := nameOfFunction(handlers.Last())30if DebugPrintRouteFunc == nil {31debugPrint("%-6s %-25s --> %s (%d handlers)\n", httpMethod, absolutePath, handlerName, nuHandlers)32} else {33DebugPrintRouteFunc(httpMethod, absolutePath, handlerName, nuHandlers)34}35}36}
37
38func debugPrintLoadTemplate(tmpl *template.Template) {39if IsDebugging() {40var buf strings.Builder41for _, tmpl := range tmpl.Templates() {42buf.WriteString("\t- ")43buf.WriteString(tmpl.Name())44buf.WriteString("\n")45}46debugPrint("Loaded HTML Templates (%d): \n%s\n", len(tmpl.Templates()), buf.String())47}48}
49
50func debugPrint(format string, values ...any) {51if IsDebugging() {52if !strings.HasSuffix(format, "\n") {53format += "\n"54}55fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)56}57}
58
59func getMinVer(v string) (uint64, error) {60first := strings.IndexByte(v, '.')61last := strings.LastIndexByte(v, '.')62if first == last {63return strconv.ParseUint(v[first+1:], 10, 64)64}65return strconv.ParseUint(v[first+1:last], 10, 64)66}
67
68func debugPrintWARNINGDefault() {69if v, e := getMinVer(runtime.Version()); e == nil && v < ginSupportMinGoVer {70debugPrint(`[WARNING] Now Gin requires Go 1.18+.71
72`)73}74debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.75
76`)77}
78
79func debugPrintWARNINGNew() {80debugPrint(`[WARNING] Running in "debug" mode. Switch to "release" mode in production.81- using env: export GIN_MODE=release
82- using code: gin.SetMode(gin.ReleaseMode)
83
84`)85}
86
87func debugPrintWARNINGSetHTMLTemplate() {88debugPrint(`[WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called89at initialization. ie. before any route is registered or the router is listening in a socket:
90
91router := gin.Default()
92router.SetHTMLTemplate(template) // << good place
93
94`)95}
96
97func debugPrintError(err error) {98if err != nil && IsDebugging() {99fmt.Fprintf(DefaultErrorWriter, "[GIN-debug] [ERROR] %v\n", err)100}101}
102