Skip to content

Commit

Permalink
feat: add flags for metrics and health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ingvaar committed May 10, 2022
1 parent 936dbd9 commit 1c9688f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ const (
FlagGasLimit = "gas-limit"
FlagNoTLS = "no-tls"
FlagTLSSkipVerify = "tls-skip-verify"
FlagMetrics = "metrics"
FlagHealth = "health"
)
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"okp4/cosmos-faucet/internal/server"
"os"

"okp4/cosmos-faucet/pkg"
Expand All @@ -20,6 +21,7 @@ const (
)

var config pkg.Config
var serverConfig server.Config

// NewRootCommand returns the root CLI command with persistent flag handling.
var rootCmd = &cobra.Command{
Expand All @@ -45,6 +47,8 @@ func Execute() {
rootCmd.PersistentFlags().StringVar(&config.Memo, FlagMemo, "Sent by økp4 faucet", "The memo description")
rootCmd.PersistentFlags().Uint64Var(&config.GasLimit, FlagGasLimit, 200000, "Gas limit")
rootCmd.PersistentFlags().BoolVar(&config.NoTLS, FlagNoTLS, false, "No encryption with the GRPC endpoint")
rootCmd.PersistentFlags().BoolVar(&serverConfig.EnableMetrics, FlagMetrics, false, "Enable metrics endpoint")
rootCmd.PersistentFlags().BoolVar(&serverConfig.EnableHealth, FlagHealth, false, "Enable health endpoint")
rootCmd.PersistentFlags().BoolVar(&config.TLSSkipVerify,
FlagTLSSkipVerify,
false,
Expand Down
2 changes: 1 addition & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewStartCommand() *cobra.Command {
log.Info().Msg("Server stopped")
}(faucet)

server.NewServer(faucet).Start(addr)
server.NewServer(serverConfig).Start(addr)
},
}

Expand Down
21 changes: 12 additions & 9 deletions internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ package server

import (
"okp4/cosmos-faucet/internal/server/handlers"
"okp4/cosmos-faucet/pkg/client"
"okp4/cosmos-faucet/pkg/server"
)

func (s *httpServer) createRoutes(faucet *client.Faucet) {
func (s *httpServer) createRoutes(config Config) {
s.router.Use(handlers.PrometheusMiddleware)
s.router.Path("/").
Queries("address", "{address}").
HandlerFunc(server.NewSendRequestHandlerFn(faucet)).
Methods("GET")
s.router.Path("/health").
HandlerFunc(handlers.NewHealthRequestHandlerFunc()).
Methods("GET")
s.router.Path("/metrics").
Handler(handlers.NewMetricsRequestHandler()).
HandlerFunc(server.NewSendRequestHandlerFn(config.Faucet)).
Methods("GET")
if config.EnableHealth {
s.router.Path("/health").
HandlerFunc(handlers.NewHealthRequestHandlerFunc()).
Methods("GET")
}
if config.EnableMetrics {
s.router.Path("/metrics").
Handler(handlers.NewMetricsRequestHandler()).
Methods("GET")
}
}
11 changes: 9 additions & 2 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import (
"github.com/rs/zerolog/log"
)

// Config holds config of the http server.
type Config struct {
EnableMetrics bool `mapstructure:"metrics"`
EnableHealth bool `mapstructure:"health"`
Faucet *client.Faucet
}

// HTTPServer exposes server methods.
type HTTPServer interface {
Start(string)
Expand All @@ -18,11 +25,11 @@ type httpServer struct {
}

// NewServer creates a new httpServer containing router.
func NewServer(faucet *client.Faucet) HTTPServer {
func NewServer(config Config) HTTPServer {
server := &httpServer{
router: mux.NewRouter().StrictSlash(true),
}
server.createRoutes(faucet)
server.createRoutes(config)
return server
}

Expand Down

0 comments on commit 1c9688f

Please sign in to comment.