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

Commit

Permalink
Prepare to v0.11.5 release, from now
Browse files Browse the repository at this point in the history
- metrics handler in container with name `metric_handler`
- profile handler in container with name `pprof_handler`
- phasing out go-convey
  • Loading branch information
im-kulikov committed Apr 14, 2019
1 parent 67e599a commit 84e35cc
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 68 deletions.
59 changes: 51 additions & 8 deletions web/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package web

import (
"net/http"
"net/http/pprof"

"github.com/chapsuk/mserv"
"github.com/im-kulikov/helium/logger"
Expand Down Expand Up @@ -35,14 +36,44 @@ type (

Server mserv.Server `group:"web_server"`
}

pprofParams struct {
dig.In

Handler http.Handler `name:"profile_handler"`
Viper *viper.Viper
Logger logger.StdLogger
}

pprofResult struct {
dig.Out

Handler http.Handler `name:"profile_handler"`
}

metricParams struct {
dig.In

Handler http.Handler `name:"metric_handler"`
Viper *viper.Viper
Logger logger.StdLogger
}

metricResult struct {
dig.Out

Handler http.Handler `name:"metric_handler"`
}
)

var (
// ServersModule of web base structs
ServersModule = module.Module{
{Constructor: newProfileHandler},
{Constructor: newProfileServer},
{Constructor: newMetricHandler},
{Constructor: newMetricServer},
{Constructor: NewAPIServer},
{Constructor: NewMetricsServer},
{Constructor: NewPprofServer},
{Constructor: NewMultiServer},
}
)
Expand All @@ -53,14 +84,26 @@ func NewMultiServer(params MultiServerParams) mserv.Server {
return mserv.New(params.Servers...)
}

// NewPprofServer returns wrapped pprof http server
func NewPprofServer(v *viper.Viper, l logger.StdLogger) ServerResult {
return newHTTPServer(v, "pprof", http.DefaultServeMux, l)
func newProfileHandler() pprofResult {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
return pprofResult{Handler: mux}
}

func newProfileServer(p pprofParams) ServerResult {
return newHTTPServer(p.Viper, "pprof", p.Handler, p.Logger)
}

func newMetricHandler() metricResult {
return metricResult{Handler: promhttp.Handler()}
}

// NewMetricsServer returns wrapped prometheus http server
func NewMetricsServer(v *viper.Viper, l logger.StdLogger) ServerResult {
return newHTTPServer(v, "metrics", promhttp.Handler(), l)
func newMetricServer(p metricParams) ServerResult {
return newHTTPServer(p.Viper, "metrics", p.Handler, p.Logger)
}

// NewAPIServer creates api server by http.Handler from DI container
Expand Down
142 changes: 82 additions & 60 deletions web/servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/chapsuk/mserv"
"github.com/im-kulikov/helium/logger"
"github.com/im-kulikov/helium/module"
. "github.com/smartystreets/goconvey/convey"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"go.uber.org/dig"
"go.uber.org/zap"
)
Expand All @@ -18,78 +18,100 @@ func testHTTPHandler() http.Handler {
}

func TestServers(t *testing.T) {
Convey("Servers test suite", t, func(c C) {
v := viper.New()
z := zap.L()
l := logger.NewStdLogger(z)
di := dig.New()
var (
z = zap.L()
di = dig.New()
v = viper.New()
l = logger.NewStdLogger(z)
)

c.Convey("check pprof server", func(c C) {
c.Convey("without config", func(c C) {
serve := NewPprofServer(v, l)
c.So(serve.Server, ShouldBeNil)
})

c.Convey("with config", func(c C) {
v.SetDefault("pprof.address", ":6090")
serve := NewPprofServer(v, l)
c.So(serve.Server, ShouldNotBeNil)
})
t.Run("check pprof server", func(t *testing.T) {
t.Run("without config", func(t *testing.T) {
params := pprofParams{
Viper: v,
Logger: l,
Handler: newProfileHandler().Handler,
}
serve := newProfileServer(params)
assert.Nil(t, serve.Server)
})

c.Convey("check metrics server", func(c C) {
c.Convey("without config", func(c C) {
serve := NewMetricsServer(v, l)
c.So(serve.Server, ShouldBeNil)
})
t.Run("with config", func(t *testing.T) {
v.SetDefault("pprof.address", ":6090")
params := pprofParams{
Viper: v,
Logger: l,
Handler: newProfileHandler().Handler,
}
serve := newProfileServer(params)
assert.NotNil(t, serve.Server)
})
})

c.Convey("with config", func(c C) {
v.SetDefault("metrics.address", ":8090")
serve := NewMetricsServer(v, l)
c.So(serve.Server, ShouldNotBeNil)
})
t.Run("check metrics server", func(t *testing.T) {
t.Run("without config", func(t *testing.T) {
params := metricParams{
Viper: v,
Logger: l,
Handler: newMetricHandler().Handler,
}
serve := newMetricServer(params)
assert.Nil(t, serve.Server)
})

c.Convey("check api server", func(c C) {
c.Convey("without config", func(c C) {
serve := NewAPIServer(v, l, nil)
c.So(serve.Server, ShouldBeNil)
})
t.Run("with config", func(t *testing.T) {
v.SetDefault("metrics.address", ":8090")
params := metricParams{
Viper: v,
Logger: l,
Handler: newMetricHandler().Handler,
}
serve := newMetricServer(params)
assert.NotNil(t, serve.Server)
})
})

c.Convey("without handler", func(c C) {
v.SetDefault("api.address", ":8090")
serve := NewAPIServer(v, l, nil)
c.So(serve.Server, ShouldBeNil)
})
t.Run("check api server", func(t *testing.T) {
t.Run("without config", func(t *testing.T) {
serve := NewAPIServer(v, l, nil)
assert.Nil(t, serve.Server)
})

c.Convey("should be ok", func(c C) {
v.SetDefault("api.address", ":8090")
serve := NewAPIServer(v, l, testHTTPHandler())
c.So(serve.Server, ShouldNotBeNil)
})
t.Run("without handler", func(t *testing.T) {
v.SetDefault("api.address", ":8090")
serve := NewAPIServer(v, l, nil)
assert.Nil(t, serve.Server)
})

c.Convey("check multi server", func(c C) {
v.SetDefault("pprof.address", ":6090")
v.SetDefault("metrics.address", ":8090")
t.Run("should be ok", func(t *testing.T) {
v.SetDefault("api.address", ":8090")
serve := NewAPIServer(v, l, testHTTPHandler())
assert.NotNil(t, serve.Server)
})
})

mod := module.Module{
{Constructor: NewPprofServer},
{Constructor: NewMetricsServer},
{Constructor: NewAPIServer},
{Constructor: NewMultiServer},
{Constructor: func() *viper.Viper { return v }},
{Constructor: func() logger.StdLogger { return l }},
{Constructor: func() http.Handler { return testHTTPHandler() }},
}
t.Run("check multi server", func(t *testing.T) {
v.SetDefault("pprof.address", ":6090")
v.SetDefault("metrics.address", ":8090")
v.SetDefault("api.address", ":8090")

mod := module.Module{
{Constructor: newProfileHandler},
{Constructor: newProfileServer},
{Constructor: newMetricHandler},
{Constructor: newMetricServer},
{Constructor: NewAPIServer},
{Constructor: NewMultiServer},
{Constructor: func() *viper.Viper { return v }},
{Constructor: func() logger.StdLogger { return l }},
{Constructor: func() http.Handler { return testHTTPHandler() }},
}

err := module.Provide(di, mod)
c.So(err, ShouldBeNil)
err = di.Invoke(func(serve mserv.Server) {
c.So(serve, ShouldHaveSameTypeAs, &mserv.MultiServer{})
})
c.So(err, ShouldBeNil)
err := module.Provide(di, mod)
assert.NoError(t, err)
err = di.Invoke(func(serve mserv.Server) {
assert.IsType(t, &mserv.MultiServer{}, serve)
})
assert.NoError(t, err)
})
}

0 comments on commit 84e35cc

Please sign in to comment.