-
Notifications
You must be signed in to change notification settings - Fork 12
/
gin.go
39 lines (33 loc) · 1.08 KB
/
gin.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
// Copyright 2022 The searKing Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webserver
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/searKing/golang/pkg/webserver/healthz"
gin_ "github.com/searKing/golang/third_party/github.com/gin-gonic/gin"
)
type ginMuxer struct {
muxer gin.IRouter
}
func GinMuxer(muxer gin.IRouter) healthz.Muxer {
return &ginMuxer{muxer: muxer}
}
func (mux *ginMuxer) Handle(pattern string, handler http.Handler) {
mux.muxer.GET(pattern, gin.WrapH(handler))
}
// GinLogFormatter is the log format function [gin.Logger] middleware uses.
func GinLogFormatter(layout string) func(param gin.LogFormatterParams) string {
return gin_.LogFormatterWithExtra(layout, func(param gin.LogFormatterParams) string {
if param.Request != nil {
fields := logging.ExtractFields(param.Request.Context())
if len(fields) > 0 {
return fmt.Sprintf(" | %v", fields)
}
}
return ""
})
}