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

Commit 84e35cc

Browse files
committed
Prepare to v0.11.5 release, from now
- metrics handler in container with name `metric_handler` - profile handler in container with name `pprof_handler` - phasing out go-convey
1 parent 67e599a commit 84e35cc

File tree

2 files changed

+133
-68
lines changed

2 files changed

+133
-68
lines changed

web/servers.go

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package web
22

33
import (
44
"net/http"
5+
"net/http/pprof"
56

67
"github.com/chapsuk/mserv"
78
"github.com/im-kulikov/helium/logger"
@@ -35,14 +36,44 @@ type (
3536

3637
Server mserv.Server `group:"web_server"`
3738
}
39+
40+
pprofParams struct {
41+
dig.In
42+
43+
Handler http.Handler `name:"profile_handler"`
44+
Viper *viper.Viper
45+
Logger logger.StdLogger
46+
}
47+
48+
pprofResult struct {
49+
dig.Out
50+
51+
Handler http.Handler `name:"profile_handler"`
52+
}
53+
54+
metricParams struct {
55+
dig.In
56+
57+
Handler http.Handler `name:"metric_handler"`
58+
Viper *viper.Viper
59+
Logger logger.StdLogger
60+
}
61+
62+
metricResult struct {
63+
dig.Out
64+
65+
Handler http.Handler `name:"metric_handler"`
66+
}
3867
)
3968

4069
var (
4170
// ServersModule of web base structs
4271
ServersModule = module.Module{
72+
{Constructor: newProfileHandler},
73+
{Constructor: newProfileServer},
74+
{Constructor: newMetricHandler},
75+
{Constructor: newMetricServer},
4376
{Constructor: NewAPIServer},
44-
{Constructor: NewMetricsServer},
45-
{Constructor: NewPprofServer},
4677
{Constructor: NewMultiServer},
4778
}
4879
)
@@ -53,14 +84,26 @@ func NewMultiServer(params MultiServerParams) mserv.Server {
5384
return mserv.New(params.Servers...)
5485
}
5586

56-
// NewPprofServer returns wrapped pprof http server
57-
func NewPprofServer(v *viper.Viper, l logger.StdLogger) ServerResult {
58-
return newHTTPServer(v, "pprof", http.DefaultServeMux, l)
87+
func newProfileHandler() pprofResult {
88+
mux := http.NewServeMux()
89+
mux.HandleFunc("/debug/pprof/", pprof.Index)
90+
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
91+
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
92+
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
93+
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
94+
return pprofResult{Handler: mux}
95+
}
96+
97+
func newProfileServer(p pprofParams) ServerResult {
98+
return newHTTPServer(p.Viper, "pprof", p.Handler, p.Logger)
99+
}
100+
101+
func newMetricHandler() metricResult {
102+
return metricResult{Handler: promhttp.Handler()}
59103
}
60104

61-
// NewMetricsServer returns wrapped prometheus http server
62-
func NewMetricsServer(v *viper.Viper, l logger.StdLogger) ServerResult {
63-
return newHTTPServer(v, "metrics", promhttp.Handler(), l)
105+
func newMetricServer(p metricParams) ServerResult {
106+
return newHTTPServer(p.Viper, "metrics", p.Handler, p.Logger)
64107
}
65108

66109
// NewAPIServer creates api server by http.Handler from DI container

web/servers_test.go

Lines changed: 82 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"github.com/chapsuk/mserv"
88
"github.com/im-kulikov/helium/logger"
99
"github.com/im-kulikov/helium/module"
10-
. "github.com/smartystreets/goconvey/convey"
1110
"github.com/spf13/viper"
11+
"github.com/stretchr/testify/assert"
1212
"go.uber.org/dig"
1313
"go.uber.org/zap"
1414
)
@@ -18,78 +18,100 @@ func testHTTPHandler() http.Handler {
1818
}
1919

2020
func TestServers(t *testing.T) {
21-
Convey("Servers test suite", t, func(c C) {
22-
v := viper.New()
23-
z := zap.L()
24-
l := logger.NewStdLogger(z)
25-
di := dig.New()
21+
var (
22+
z = zap.L()
23+
di = dig.New()
24+
v = viper.New()
25+
l = logger.NewStdLogger(z)
26+
)
2627

27-
c.Convey("check pprof server", func(c C) {
28-
c.Convey("without config", func(c C) {
29-
serve := NewPprofServer(v, l)
30-
c.So(serve.Server, ShouldBeNil)
31-
})
32-
33-
c.Convey("with config", func(c C) {
34-
v.SetDefault("pprof.address", ":6090")
35-
serve := NewPprofServer(v, l)
36-
c.So(serve.Server, ShouldNotBeNil)
37-
})
28+
t.Run("check pprof server", func(t *testing.T) {
29+
t.Run("without config", func(t *testing.T) {
30+
params := pprofParams{
31+
Viper: v,
32+
Logger: l,
33+
Handler: newProfileHandler().Handler,
34+
}
35+
serve := newProfileServer(params)
36+
assert.Nil(t, serve.Server)
3837
})
3938

40-
c.Convey("check metrics server", func(c C) {
41-
c.Convey("without config", func(c C) {
42-
serve := NewMetricsServer(v, l)
43-
c.So(serve.Server, ShouldBeNil)
44-
})
39+
t.Run("with config", func(t *testing.T) {
40+
v.SetDefault("pprof.address", ":6090")
41+
params := pprofParams{
42+
Viper: v,
43+
Logger: l,
44+
Handler: newProfileHandler().Handler,
45+
}
46+
serve := newProfileServer(params)
47+
assert.NotNil(t, serve.Server)
48+
})
49+
})
4550

46-
c.Convey("with config", func(c C) {
47-
v.SetDefault("metrics.address", ":8090")
48-
serve := NewMetricsServer(v, l)
49-
c.So(serve.Server, ShouldNotBeNil)
50-
})
51+
t.Run("check metrics server", func(t *testing.T) {
52+
t.Run("without config", func(t *testing.T) {
53+
params := metricParams{
54+
Viper: v,
55+
Logger: l,
56+
Handler: newMetricHandler().Handler,
57+
}
58+
serve := newMetricServer(params)
59+
assert.Nil(t, serve.Server)
5160
})
5261

53-
c.Convey("check api server", func(c C) {
54-
c.Convey("without config", func(c C) {
55-
serve := NewAPIServer(v, l, nil)
56-
c.So(serve.Server, ShouldBeNil)
57-
})
62+
t.Run("with config", func(t *testing.T) {
63+
v.SetDefault("metrics.address", ":8090")
64+
params := metricParams{
65+
Viper: v,
66+
Logger: l,
67+
Handler: newMetricHandler().Handler,
68+
}
69+
serve := newMetricServer(params)
70+
assert.NotNil(t, serve.Server)
71+
})
72+
})
5873

59-
c.Convey("without handler", func(c C) {
60-
v.SetDefault("api.address", ":8090")
61-
serve := NewAPIServer(v, l, nil)
62-
c.So(serve.Server, ShouldBeNil)
63-
})
74+
t.Run("check api server", func(t *testing.T) {
75+
t.Run("without config", func(t *testing.T) {
76+
serve := NewAPIServer(v, l, nil)
77+
assert.Nil(t, serve.Server)
78+
})
6479

65-
c.Convey("should be ok", func(c C) {
66-
v.SetDefault("api.address", ":8090")
67-
serve := NewAPIServer(v, l, testHTTPHandler())
68-
c.So(serve.Server, ShouldNotBeNil)
69-
})
80+
t.Run("without handler", func(t *testing.T) {
81+
v.SetDefault("api.address", ":8090")
82+
serve := NewAPIServer(v, l, nil)
83+
assert.Nil(t, serve.Server)
7084
})
7185

72-
c.Convey("check multi server", func(c C) {
73-
v.SetDefault("pprof.address", ":6090")
74-
v.SetDefault("metrics.address", ":8090")
86+
t.Run("should be ok", func(t *testing.T) {
7587
v.SetDefault("api.address", ":8090")
88+
serve := NewAPIServer(v, l, testHTTPHandler())
89+
assert.NotNil(t, serve.Server)
90+
})
91+
})
7692

77-
mod := module.Module{
78-
{Constructor: NewPprofServer},
79-
{Constructor: NewMetricsServer},
80-
{Constructor: NewAPIServer},
81-
{Constructor: NewMultiServer},
82-
{Constructor: func() *viper.Viper { return v }},
83-
{Constructor: func() logger.StdLogger { return l }},
84-
{Constructor: func() http.Handler { return testHTTPHandler() }},
85-
}
93+
t.Run("check multi server", func(t *testing.T) {
94+
v.SetDefault("pprof.address", ":6090")
95+
v.SetDefault("metrics.address", ":8090")
96+
v.SetDefault("api.address", ":8090")
97+
98+
mod := module.Module{
99+
{Constructor: newProfileHandler},
100+
{Constructor: newProfileServer},
101+
{Constructor: newMetricHandler},
102+
{Constructor: newMetricServer},
103+
{Constructor: NewAPIServer},
104+
{Constructor: NewMultiServer},
105+
{Constructor: func() *viper.Viper { return v }},
106+
{Constructor: func() logger.StdLogger { return l }},
107+
{Constructor: func() http.Handler { return testHTTPHandler() }},
108+
}
86109

87-
err := module.Provide(di, mod)
88-
c.So(err, ShouldBeNil)
89-
err = di.Invoke(func(serve mserv.Server) {
90-
c.So(serve, ShouldHaveSameTypeAs, &mserv.MultiServer{})
91-
})
92-
c.So(err, ShouldBeNil)
110+
err := module.Provide(di, mod)
111+
assert.NoError(t, err)
112+
err = di.Invoke(func(serve mserv.Server) {
113+
assert.IsType(t, &mserv.MultiServer{}, serve)
93114
})
115+
assert.NoError(t, err)
94116
})
95117
}

0 commit comments

Comments
 (0)