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.6 release, from now
Browse files Browse the repository at this point in the history
- metrics handler is optional and moved to MetricHandlerModule
- profile handler is optional and moved to ProfileHandlerModule
- reorder if-statement in newHTTPServer
  • Loading branch information
im-kulikov committed Apr 16, 2019
1 parent 84e35cc commit 83bcf72
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
34 changes: 21 additions & 13 deletions web/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ type (
Server mserv.Server `group:"web_server"`
}

pprofParams struct {
profileParams struct {
dig.In

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

pprofResult struct {
profileResult struct {
dig.Out

Handler http.Handler `name:"profile_handler"`
Expand All @@ -54,7 +54,7 @@ type (
metricParams struct {
dig.In

Handler http.Handler `name:"metric_handler"`
Handler http.Handler `name:"metric_handler" optional:"true"`
Viper *viper.Viper
Logger logger.StdLogger
}
Expand All @@ -67,11 +67,19 @@ type (
)

var (
// ProfileHandlerModule that provides default profile handler
ProfileHandlerModule = module.Module{
{Constructor: newProfileHandler},
}

// MetricHandlerModule that provides default metric handler
MetricHandlerModule = module.Module{
{Constructor: newMetricHandler},
}

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

func newProfileHandler() pprofResult {
func newProfileHandler() profileResult {
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}
return profileResult{Handler: mux}
}

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

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

func newHTTPServer(v *viper.Viper, key string, h http.Handler, l logger.StdLogger) ServerResult {
if !v.IsSet(key + ".address") {
l.Printf("Empty bind address for %s server, skip", key)
return ServerResult{}
}
if h == nil {
l.Printf("Empty handler for %s server, skip", key)
return ServerResult{}
} else if !v.IsSet(key + ".address") {
l.Printf("Empty bind address for %s server, skip", key)
return ServerResult{}
}

l.Printf("Create %s http server, bind address: %s", key, v.GetString(key+".address"))
return ServerResult{
Server: mserv.NewHTTPServer(
Expand Down
16 changes: 7 additions & 9 deletions web/servers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestServers(t *testing.T) {

t.Run("check pprof server", func(t *testing.T) {
t.Run("without config", func(t *testing.T) {
params := pprofParams{
params := profileParams{
Viper: v,
Logger: l,
Handler: newProfileHandler().Handler,
Expand All @@ -38,7 +38,7 @@ func TestServers(t *testing.T) {

t.Run("with config", func(t *testing.T) {
v.SetDefault("pprof.address", ":6090")
params := pprofParams{
params := profileParams{
Viper: v,
Logger: l,
Handler: newProfileHandler().Handler,
Expand Down Expand Up @@ -96,16 +96,14 @@ func TestServers(t *testing.T) {
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() }},
}
}.Append(
ServersModule,
ProfileHandlerModule,
MetricHandlerModule,
)

err := module.Provide(di, mod)
assert.NoError(t, err)
Expand Down

0 comments on commit 83bcf72

Please sign in to comment.