Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit 83bcf72

Browse files
committed
Prepare to v0.11.6 release, from now
- metrics handler is optional and moved to MetricHandlerModule - profile handler is optional and moved to ProfileHandlerModule - reorder if-statement in newHTTPServer
1 parent 84e35cc commit 83bcf72

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

web/servers.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ type (
3737
Server mserv.Server `group:"web_server"`
3838
}
3939

40-
pprofParams struct {
40+
profileParams struct {
4141
dig.In
4242

43-
Handler http.Handler `name:"profile_handler"`
43+
Handler http.Handler `name:"profile_handler" optional:"true"`
4444
Viper *viper.Viper
4545
Logger logger.StdLogger
4646
}
4747

48-
pprofResult struct {
48+
profileResult struct {
4949
dig.Out
5050

5151
Handler http.Handler `name:"profile_handler"`
@@ -54,7 +54,7 @@ type (
5454
metricParams struct {
5555
dig.In
5656

57-
Handler http.Handler `name:"metric_handler"`
57+
Handler http.Handler `name:"metric_handler" optional:"true"`
5858
Viper *viper.Viper
5959
Logger logger.StdLogger
6060
}
@@ -67,11 +67,19 @@ type (
6767
)
6868

6969
var (
70+
// ProfileHandlerModule that provides default profile handler
71+
ProfileHandlerModule = module.Module{
72+
{Constructor: newProfileHandler},
73+
}
74+
75+
// MetricHandlerModule that provides default metric handler
76+
MetricHandlerModule = module.Module{
77+
{Constructor: newMetricHandler},
78+
}
79+
7080
// ServersModule of web base structs
7181
ServersModule = module.Module{
72-
{Constructor: newProfileHandler},
7382
{Constructor: newProfileServer},
74-
{Constructor: newMetricHandler},
7583
{Constructor: newMetricServer},
7684
{Constructor: NewAPIServer},
7785
{Constructor: NewMultiServer},
@@ -84,17 +92,17 @@ func NewMultiServer(params MultiServerParams) mserv.Server {
8492
return mserv.New(params.Servers...)
8593
}
8694

87-
func newProfileHandler() pprofResult {
95+
func newProfileHandler() profileResult {
8896
mux := http.NewServeMux()
8997
mux.HandleFunc("/debug/pprof/", pprof.Index)
9098
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
9199
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
92100
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
93101
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
94-
return pprofResult{Handler: mux}
102+
return profileResult{Handler: mux}
95103
}
96104

97-
func newProfileServer(p pprofParams) ServerResult {
105+
func newProfileServer(p profileParams) ServerResult {
98106
return newHTTPServer(p.Viper, "pprof", p.Handler, p.Logger)
99107
}
100108

@@ -112,14 +120,14 @@ func NewAPIServer(v *viper.Viper, l logger.StdLogger, h http.Handler) ServerResu
112120
}
113121

114122
func newHTTPServer(v *viper.Viper, key string, h http.Handler, l logger.StdLogger) ServerResult {
115-
if !v.IsSet(key + ".address") {
116-
l.Printf("Empty bind address for %s server, skip", key)
117-
return ServerResult{}
118-
}
119123
if h == nil {
120124
l.Printf("Empty handler for %s server, skip", key)
121125
return ServerResult{}
126+
} else if !v.IsSet(key + ".address") {
127+
l.Printf("Empty bind address for %s server, skip", key)
128+
return ServerResult{}
122129
}
130+
123131
l.Printf("Create %s http server, bind address: %s", key, v.GetString(key+".address"))
124132
return ServerResult{
125133
Server: mserv.NewHTTPServer(

web/servers_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestServers(t *testing.T) {
2727

2828
t.Run("check pprof server", func(t *testing.T) {
2929
t.Run("without config", func(t *testing.T) {
30-
params := pprofParams{
30+
params := profileParams{
3131
Viper: v,
3232
Logger: l,
3333
Handler: newProfileHandler().Handler,
@@ -38,7 +38,7 @@ func TestServers(t *testing.T) {
3838

3939
t.Run("with config", func(t *testing.T) {
4040
v.SetDefault("pprof.address", ":6090")
41-
params := pprofParams{
41+
params := profileParams{
4242
Viper: v,
4343
Logger: l,
4444
Handler: newProfileHandler().Handler,
@@ -96,16 +96,14 @@ func TestServers(t *testing.T) {
9696
v.SetDefault("api.address", ":8090")
9797

9898
mod := module.Module{
99-
{Constructor: newProfileHandler},
100-
{Constructor: newProfileServer},
101-
{Constructor: newMetricHandler},
102-
{Constructor: newMetricServer},
103-
{Constructor: NewAPIServer},
104-
{Constructor: NewMultiServer},
10599
{Constructor: func() *viper.Viper { return v }},
106100
{Constructor: func() logger.StdLogger { return l }},
107101
{Constructor: func() http.Handler { return testHTTPHandler() }},
108-
}
102+
}.Append(
103+
ServersModule,
104+
ProfileHandlerModule,
105+
MetricHandlerModule,
106+
)
109107

110108
err := module.Provide(di, mod)
111109
assert.NoError(t, err)

0 commit comments

Comments
 (0)