Skip to content

Commit

Permalink
Merge pull request #224 from bitcoin-sv/fix/BUX-667-display-version-i…
Browse files Browse the repository at this point in the history
…n-swagger

fix(BUX-667): display application version in swagger.
  • Loading branch information
dorzepowski authored Mar 14, 2024
2 parents f561d03 + 6ca2963 commit a49e6bf
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 141 deletions.
3 changes: 1 addition & 2 deletions cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/bitcoin-sv/block-headers-service/config"
"github.com/bitcoin-sv/block-headers-service/database"
"github.com/bitcoin-sv/block-headers-service/logging"
"github.com/bitcoin-sv/block-headers-service/version"
)

type cliFlags struct {
Expand Down Expand Up @@ -68,7 +67,7 @@ func parseCliFlags(cli *cliFlags, cfg *config.AppConfig, appFlags *pflag.FlagSet
}

if cli.showVersion {
log.Info().Msgf("block headers service version %s", version.String())
fmt.Println(config.ApplicationName, config.Version())
os.Exit(0)
}

Expand Down
9 changes: 6 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ import (
httpserver "github.com/bitcoin-sv/block-headers-service/transports/http/server"
"github.com/bitcoin-sv/block-headers-service/transports/p2p"
peerpkg "github.com/bitcoin-sv/block-headers-service/transports/p2p/peer"
"github.com/bitcoin-sv/block-headers-service/version"

sqlrepository "github.com/bitcoin-sv/block-headers-service/database/repository"
)

// version version of the application that can be overridden with ldflags during build
// (e.g. go build -ldflags "-X main.version=1.2.3").
var version = "development"

// nolint: godot
// @securityDefinitions.apikey Bearer
// @in header
// @name Authorization
func main() {
defaultLog := logging.GetDefaultLogger()

if err := config.SetDefaults(defaultLog); err != nil {
if err := config.SetDefaults(version, defaultLog); err != nil {
defaultLog.Error().Msgf("cannot set config default values: %v", err)
}

Expand Down Expand Up @@ -89,7 +92,7 @@ func main() {
wire.SetLimits(config.ExcessiveBlockSize)

// Show version at startup.
log.Info().Msgf("Version %s", version.String())
log.Info().Msgf("Version %s", config.Version())

peers := make(map[*peerpkg.Peer]*peerpkg.PeerSyncState)

Expand Down
12 changes: 9 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
)

const (
ApplicationName = "Block Headers Service"
APIVersion = "v1"
Version = "v0.6.0"
ApplicationName = "block-headers-service"
ConfigFilePathKey = "config_file"
DefaultConfigFilePath = "config.yaml"
ConfigEnvPrefix = "bhs"
)

var version = "should-be-overridden-by-setDefaults"

var Lookup func(string) ([]net.IP, error)
var Dial func(string, string, time.Duration) (net.Conn, error)
var Checkpoints []chaincfg.Checkpoint
Expand All @@ -32,6 +32,10 @@ const (
DBPostgreSql DbEngine = "postgres"
)

func Version() string {
return version
}

// AppConfig returns strongly typed config values.
type AppConfig struct {
Db *DbConfig `mapstructure:"db"`
Expand Down Expand Up @@ -119,6 +123,8 @@ type P2PConfig struct {
BlocksForForkConfirmation int `mapstructure:"blocks_for_confirmation" description:"Minimum number of blocks to consider a block confirmed"`
// DefaultConnectTimeout is the default connection timeout.
DefaultConnectTimeout time.Duration `mapstructure:"default_connect_timeout" description:"The default connection timeout"`
UserAgentName string `mapstructure:"user_agent_name" description:"The name that should be used during announcement of the client on the p2p network"`
UserAgentVersion string `mapstructure:"user_agent_version" description:"By default will be equal to application version, but can be overridden for development purposes"`
}

// LoggingConfig represents a logging config.
Expand Down
4 changes: 3 additions & 1 deletion config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ func getP2PDefaults() *P2PConfig {
BlocksForForkConfirmation: 10,
DefaultConnectTimeout: 30 * time.Second,
DisableCheckpoints: false,
UserAgentName: ApplicationName,
UserAgentVersion: Version(),
}
}

func getLoggingDefaults() *LoggingConfig {
return &LoggingConfig{
Level: "debug",
Format: "console",
InstanceName: "block-headers-service",
InstanceName: ApplicationName,
LogOrigin: true,
}
}
Expand Down
9 changes: 5 additions & 4 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package config
import (
"fmt"
"net"
"os"
"strings"
"sync"

"github.com/bitcoin-sv/block-headers-service/logging"
"github.com/rs/zerolog"

"os"

"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -40,9 +39,11 @@ func Load(cfg *AppConfig) (*AppConfig, *zerolog.Logger, error) {
return cfg, logger, nil
}

func SetDefaults(log *zerolog.Logger) error {
func SetDefaults(defaultVersion string, log *zerolog.Logger) error {
viperLock.Lock()
defer viperLock.Unlock()
viper.SetDefault(ConfigFilePathKey, DefaultConfigFilePath)

version = defaultVersion
defaultsMap := make(map[string]interface{})
if err := mapstructure.Decode(GetDefaultAppConfig(), &defaultsMap); err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/tests/testapp/test_bhs.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func NewTestBlockHeaderService(t *testing.T, ops ...bhsOpt) (*TestBlockHeaderSer

viper.Reset()
testLog := zerolog.Nop()
if err := config.SetDefaults(&testLog); err != nil {
if err := config.SetDefaults("unittest", &testLog); err != nil {
panic(fmt.Sprintf("cannot set config default values: %v", err))
}
defaultConfig := config.GetDefaultAppConfig()
Expand Down
4 changes: 3 additions & 1 deletion transports/http/endpoints/swagger/endpoints.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package swagger

import (
"github.com/bitcoin-sv/block-headers-service/config"
"github.com/bitcoin-sv/block-headers-service/docs"
"github.com/bitcoin-sv/block-headers-service/service"
router "github.com/bitcoin-sv/block-headers-service/transports/http/endpoints/routes"
Expand All @@ -10,9 +11,10 @@ import (
)

// NewHandler creates new endpoint handler.
func NewHandler(s *service.Services, apiUrlPrefix string) router.RootEndpoints {
func NewHandler(_ *service.Services, apiUrlPrefix string) router.RootEndpoints {
return router.RootEndpointsFunc(func(router *gin.RouterGroup) {
docs.SwaggerInfo.BasePath = apiUrlPrefix
docs.SwaggerInfo.Version = config.Version()
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
})
}
13 changes: 2 additions & 11 deletions transports/p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ const (
// retries when connecting to persistent peers. It is adjusted by the
// number of retries such that there is a retry backoff.
connectionRetryInterval = time.Second * 5

// userAgentName is the user agent name and is used to help identify
// ourselves to other bitcoin peers.
userAgentName = "/block-headers-service"

// userAgentVersion is the user agent version and is used to help
// identify ourselves to other bitcoin peers.
// userAgentVersion = fmt.Sprintf("%d.%d.%d", version.AppMajor, version.AppMinor, version.AppPatch).
userAgentVersion = "0.3.0"
)

// ServerAlreadyStarted represents starting error when a p2p server is already started.
Expand Down Expand Up @@ -882,8 +873,8 @@ func newPeerConfig(sp *serverPeer) *peer.Config {
AddrMe: addrMe,
NewestBlock: sp.newestBlock,
HostToNetAddress: sp.server.addrManager.HostToNetAddress,
UserAgentName: userAgentName,
UserAgentVersion: userAgentVersion,
UserAgentName: sp.server.p2pConfig.UserAgentName,
UserAgentVersion: sp.server.p2pConfig.UserAgentVersion,
UserAgentComments: initUserAgentComments(config.ExcessiveBlockSize),
ChainParams: sp.server.chainParams,
Services: sp.server.wireServices,
Expand Down
76 changes: 0 additions & 76 deletions version/version.go

This file was deleted.

38 changes: 0 additions & 38 deletions version/version_test.go

This file was deleted.

0 comments on commit a49e6bf

Please sign in to comment.