Skip to content

Commit

Permalink
feat(BUX-361): default config file
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-4chain committed Dec 14, 2023
1 parent c6b5c32 commit b11c98d
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 91 deletions.
28 changes: 0 additions & 28 deletions cli/cli.go

This file was deleted.

7 changes: 0 additions & 7 deletions cli/flags/flags.go

This file was deleted.

5 changes: 5 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package main

import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"time"
Expand Down Expand Up @@ -34,6 +36,9 @@ func main() {
return
}

cfg, _ := json.MarshalIndent(appConfig, "", " ")
fmt.Printf("appConfig: %s\n", cfg)

// Load the Application Services
var services *config.AppServices
if services, err = appConfig.LoadServices(context.Background()); err != nil {
Expand Down
125 changes: 125 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"auth": {
"admin_key": "xpub661MyMwAqRbcFaYeQLxmExXvTCjw9jjBRpifkoGggkAitXNNjva4TStLJuYjjEmU4AzXRPGwoECjXo3Rgqg8zQqW6UPVfkKtsrogGBw8xz7",
"require_signing": false,
"scheme": "xpub",
"signing_disabled": true
},
"beef": {
"pulse": {
"auth_token": "asd",
"url": "http://localhost:8000/api/v1/chain/merkleroot/verify"
},
"use_beef": true
},
"cache": {
"engine": "freecache"
},
"cluster": {
"coordinator": "redis",
"prefix": "bux_cluser_",
"redis": {
"max_idle_timeout": "10s",
"url": "localhost:6379",
"use_tls": false
}
},
"config_file": "",
"db": {
"datastore": {
"auto_migrate": true,
"debug": false,
"engine": "sqlite",
"table_prefix": "xapi"
},
"mongodb": {
"db_name": "xapi",
"transactions": false,
"uri": "mongodb://localhost:27017/xapi"
},
"sql": {
"driver": "postgresql",
"host": "localhost",
"name": "xapi",
"password": "",
"port": "5432",
"replica": false,
"skip_initialize_with_version": true,
"time_zone": "UTC",
"tx_timeout": "10s",
"user": "postgres"
},
"sqlite": {
"database_path": "./test-json.db",
"shared": true
}
},
"debug": true,
"debug_profiling": false,
"disable_itc": true,
"graphql": {
"enabled": true,
"playground_path": "/graphql",
"server_path": "/graphql"
},
"import_block_headers": "",
"monitor": {
"auth_token": "",
"bux_agent_url": "ws://localhost:8000/websocket",
"debug": false,
"enabled": false,
"false_positive_rate": 0.01,
"load_monitored_destinations": false,
"max_number_of_destinations": 100000,
"monitor_days": 7,
"processor_type": "bloom",
"save_transaction_destinations": true
},
"new_relic": {
"domain_name": "domain.com",
"enabled": false,
"license_key": "BOGUS-LICENSE-KEY-1234567890987654321234"
},
"nodes": {
"broadcast_client_apis": [
"url|token"
],
"minercraft_api": "mAPI",
"use_mapi_fee_quotes": true
},
"notifications": {
"enabled": false,
"webhook_endpoint": ""
},
"paymail": {
"default_from_paymail": "from@domain.com",
"default_note": "bux Address Resolution",
"domain_validation_enabled": false,
"domains": [
"localhost"
],
"enabled": true,
"sender_validation_enabled": true
},
"redis": {
"dependency_mode": true,
"max_active_connections": 0,
"max_connection_lifetime": "60s",
"max_idle_connections": 10,
"max_idle_timeout": "10s",
"url": "redis://localhost:6379",
"use_tls": false
},
"request_logging": true,
"server": {
"idle_timeout": "60s",
"port": "3003",
"read_timeout": "15s",
"write_timeout": "15s"
},
"task_manager": {
"engine": "taskq",
"factory": "memory",
"queue_name": "development_queue"
}
}
12 changes: 6 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ type AuthenticationConfig struct {

// Authentication config option keys for Viper
const (
AuthAdminKey = "authentication.admin_key"
AuthRequireSigningKey = "authentication.require_signing"
AuthSchemeKey = "authentication.scheme"
AuthSigningDisabledKey = "authentication.signing_disabled"
AuthAdminKey = "auth.admin_key"
AuthRequireSigningKey = "auth.require_signing"
AuthSchemeKey = "auth.scheme"
AuthSigningDisabledKey = "auth.signing_disabled"
)

// BeefConfig consists of components requred to use beef, e.g. Pulse for merkle roots validation
Expand Down Expand Up @@ -317,8 +317,8 @@ type PulseConfig struct {
}

const (
PulseHeaderValidationURLKey = "pulse.url"
PulseAuthTokenKey = "pulse.auth_token"
PulseHeaderValidationURLKey = "beef.pulse.url"
PulseAuthTokenKey = "beef.pulse.auth_token"
)

// GetUserAgent will return the outgoing user agent
Expand Down
25 changes: 0 additions & 25 deletions config/config.json

This file was deleted.

2 changes: 2 additions & 0 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/spf13/viper"
)

const DefaultConfigFilePath = "config.json"

// General defaults
const (
DebugDefault = true
Expand Down
63 changes: 52 additions & 11 deletions config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import (

"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/BuxOrg/bux-server/cli"
"github.com/BuxOrg/bux-server/cli/flags"
)

type buxFlags struct {
pflag.FlagSet
}

type cliFlags struct {
showVersion bool `mapstructure:"version"`
showHelp bool `mapstructure:"help"`
dumpConfig bool `mapstructure:"dump_config"`
}

func loadFlags() {
cliFlags := flags.CliFlags{}
cliFlags := cliFlags{}
buxFlags := buxFlags{}

buxFlags.initFlags(&cliFlags)
Expand All @@ -27,24 +30,62 @@ func loadFlags() {
os.Exit(1)
}

cli.ParseCliFlags(&cliFlags, Version, ConfigFilePathKey)
buxFlags.parseCliFlags(&cliFlags)

viper.BindPFlags(&buxFlags.FlagSet)
}

func (fs *buxFlags) initFlags(cliFlags *flags.CliFlags) {
func (fs *buxFlags) initFlags(cliFlags *cliFlags) {
fs.StringP(ConfigFilePathKey, "C", "", "custom config file path")

fs.initCliFlags(cliFlags)

fs.initGeneralFlags()
fs.initAuthFlags()
fs.initBeefFlags()
fs.initCachestoreFlags()
fs.initClusterFlags()
fs.initDbFlags()
fs.initGraphqlFlags()
fs.initMonitorFlags()
fs.initNewRelicFlags()
fs.initNodesFlags()
fs.initNotificationFlags()
fs.initPaymailFlags()
fs.initRedisFlags()
fs.initTaskManagerFlags()
fs.initServerFlags()
}

func (fs *buxFlags) initCliFlags(cliFlags *cliFlags) {
if cliFlags != nil {
fs.BoolVarP(&cliFlags.showHelp, "help", "h", false, "show help")
fs.BoolVarP(&cliFlags.showVersion, "version", "v", false, "show version")
fs.BoolVarP(&cliFlags.dumpConfig, "dump_config", "d", false, "dump config to file, specified by config_file flag")
}
}

func (fs *buxFlags) initCliFlags(cliFlags *flags.CliFlags) {
if cliFlags != nil {
fs.BoolVarP(&cliFlags.ShowHelp, "help", "h", false, "show help")
fs.BoolVarP(&cliFlags.ShowVersion, "version", "v", false, "show version")
fs.BoolVarP(&cliFlags.DumpConfig, "dump_config", "d", false, "dump config to file, specified by config_file flag")
func (fs *buxFlags) parseCliFlags(cli *cliFlags) {
if cli.showHelp {
fs.PrintDefaults()
os.Exit(0)
}

if cli.showVersion {
fmt.Println("bux-sever", "version", Version)
os.Exit(0)
}

if cli.dumpConfig {
configPath := viper.GetString(ConfigFilePathKey)
if configPath == "" {
configPath = "config.json"
}
err := viper.SafeWriteConfigAs(configPath)
if err != nil {
fmt.Printf("error while dumping config: %v", err.Error())
}
os.Exit(0)
}
}

Expand Down
31 changes: 17 additions & 14 deletions config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"os"
"strings"
"sync"

Expand All @@ -27,6 +28,7 @@ func Load(configFilePath string) (appConfig *AppConfig, err error) {
return nil, err
}

appConfig = new(AppConfig)
if err = unmarshallToAppConfig(appConfig); err != nil {
return nil, err
}
Expand All @@ -44,27 +46,28 @@ func envConfig() {

func loadFromFile() error {
configFilePath := viper.GetString(ConfigFilePathKey)
viper.SetConfigFile(configFilePath)

if configFilePath != "" {
viper.SetConfigFile(configFilePath)
if err := viper.ReadInConfig(); err != nil {
switch err.(type) {
case *viper.ConfigFileNotFoundError:
logger.Data(2, logger.INFO, "Config file not specified. Using defaults")
default:
err = fmt.Errorf(dictionary.GetInternalMessage(dictionary.ErrorReadingConfig), err.Error())
logger.Data(2, logger.ERROR, err.Error())
return err
}
if configFilePath == "" {
_, err := os.Stat(DefaultConfigFilePath)
if os.IsNotExist(err) {
// if the config is not specified and no default config file exists, use defaults
logger.Data(2, logger.DEBUG, "Config file not specified. Using defaults")
return nil
}
configFilePath = DefaultConfigFilePath
}

viper.SetConfigFile(configFilePath)
if err := viper.ReadInConfig(); err != nil {
err = fmt.Errorf(dictionary.GetInternalMessage(dictionary.ErrorReadingConfig), err.Error())
logger.Data(2, logger.ERROR, err.Error())
return err
}

return nil
}

func unmarshallToAppConfig(appConfig *AppConfig) error {
appConfig = new(AppConfig) // TODO: does it need to be here?

if err := viper.Unmarshal(&appConfig); err != nil {
err = fmt.Errorf(dictionary.GetInternalMessage(dictionary.ErrorViper), err.Error())
return err
Expand Down

0 comments on commit b11c98d

Please sign in to comment.