-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
110 lines (94 loc) · 2.49 KB
/
config.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
package xgin
import (
"fmt"
"github.com/gin-gonic/gin"
)
// ModName ..
const ModName = "server.gin"
// Config HTTP config
type Config struct {
Host string
Port int
Deployment string
Mode string
DisableMetric bool
DisableTrace bool
DisableSlowQuery bool
ServiceAddress string // ServiceAddress service address in registry info, default to 'Host:Port'
SlowQueryThresholdInMill int64
}
// New ...
func New() *Config {
return &Config{
Mode: gin.ReleaseMode,
SlowQueryThresholdInMill: 500, // 500ms
}
}
// WithHost ...
func (config *Config) WithHost(host string) *Config {
config.Host = host
return config
}
// WithPort ...
func (config *Config) WithPort(port int) *Config {
config.Port = port
return config
}
// WithDeployment ...
func (config *Config) WithDeployment(deployment string) *Config {
config.Deployment = deployment
return config
}
// WithMode ...
func (config *Config) WithMode(mode string) *Config {
config.Mode = mode
return config
}
// WithDisableSlowQuery ...
func (config *Config) WithDisableSlowQuery(disableSlowQuery bool) *Config {
config.DisableSlowQuery = disableSlowQuery
return config
}
// WithDisableMetric ...
func (config *Config) WithDisableMetric(disableMetric bool) *Config {
config.DisableMetric = disableMetric
return config
}
// WithDisableTrace ...
func (config *Config) WithDisableTrace(disableTrace bool) *Config {
config.DisableTrace = disableTrace
return config
}
// WithServiceAddress ...
func (config *Config) WithServiceAddress(serviceAddress string) *Config {
config.ServiceAddress = serviceAddress
return config
}
// WithSlowQueryThresholdInMilli WithPort ...
func (config *Config) WithSlowQueryThresholdInMilli(milli int64) *Config {
config.SlowQueryThresholdInMill = milli
return config
}
// Build create server instance, then initialize it with necessary interceptor
func (config *Config) Build() *Server {
server := newServer(config)
if config.Mode == gin.DebugMode {
server.Use(gin.Logger())
}
server.Use(gin.Recovery())
if !config.DisableSlowQuery {
//慢日志查询
server.Use(recoverMiddleware(config.SlowQueryThresholdInMill))
}
if !config.DisableMetric {
server.Use(metricServerInterceptor())
}
if !config.DisableTrace {
server.Use(traceServerInterceptor())
}
return server
}
// Address ...
func (config *Config) Address() string {
return fmt.Sprintf("%s:%d", config.Host, config.Port)
}