-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.go
112 lines (94 loc) · 2.71 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package gin
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/goravel/framework/contracts/config"
httpcontract "github.com/goravel/framework/contracts/http"
)
func pathToGinPath(relativePath string) string {
return bracketToColon(relativePath)
}
func middlewaresToGinHandlers(middlewares []httpcontract.Middleware) []gin.HandlerFunc {
var ginHandlers []gin.HandlerFunc
for _, item := range middlewares {
ginHandlers = append(ginHandlers, middlewareToGinHandler(item))
}
return ginHandlers
}
func handlerToGinHandler(handler httpcontract.HandlerFunc) gin.HandlerFunc {
return func(c *gin.Context) {
context := NewContext(c)
defer func() {
contextRequestPool.Put(context.request)
contextResponsePool.Put(context.response)
context.request = nil
context.response = nil
contextPool.Put(context)
}()
if response := handler(context); response != nil {
_ = response.Render()
}
}
}
func middlewareToGinHandler(middleware httpcontract.Middleware) gin.HandlerFunc {
return func(c *gin.Context) {
context := NewContext(c)
defer func() {
contextRequestPool.Put(context.request)
contextResponsePool.Put(context.response)
context.request = nil
context.response = nil
contextPool.Put(context)
}()
middleware(context)
}
}
func getDebugLog(config config.Config) gin.HandlerFunc {
logFormatter := func(param gin.LogFormatterParams) string {
var statusColor, methodColor, resetColor string
if param.IsOutputColor() {
statusColor = param.StatusCodeColor()
methodColor = param.MethodColor()
resetColor = param.ResetColor()
}
if param.Latency > time.Minute {
// Truncate in a golang < 1.8 safe way
param.Latency = param.Latency - param.Latency%time.Second
}
return fmt.Sprintf("[HTTP] %v |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n%s",
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
statusColor, param.StatusCode, resetColor,
param.Latency,
param.ClientIP,
methodColor, param.Method, resetColor,
param.Path,
param.ErrorMessage,
)
}
if config.GetBool("app.debug") {
return gin.LoggerWithFormatter(logFormatter)
}
return nil
}
func colonToBracket(relativePath string) string {
arr := strings.Split(relativePath, "/")
var newArr []string
for _, item := range arr {
if strings.HasPrefix(item, ":") {
item = "{" + strings.ReplaceAll(item, ":", "") + "}"
}
newArr = append(newArr, item)
}
return strings.Join(newArr, "/")
}
func bracketToColon(relativePath string) string {
compileRegex := regexp.MustCompile(`{(.*?)}`)
matchArr := compileRegex.FindAllStringSubmatch(relativePath, -1)
for _, item := range matchArr {
relativePath = strings.ReplaceAll(relativePath, item[0], ":"+item[1])
}
return relativePath
}