From 72fc9afa4083cd9ad693e5ab5ca58173afa279af Mon Sep 17 00:00:00 2001 From: Jamie Holdstock Date: Wed, 20 May 2020 15:31:10 +0100 Subject: [PATCH] Config for web server release/debug mode (#40) --- config.go | 39 +++++++++++++++++++++------------------ main.go | 5 +---- webapi/server.go | 10 +++++----- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/config.go b/config.go index 533b414..2cbaebe 100644 --- a/config.go +++ b/config.go @@ -25,21 +25,23 @@ var ( defaultConfigFilename = "dcrvsp.conf" defaultConfigFile = filepath.Join(defaultHomeDir, defaultConfigFilename) defaultWalletHost = "127.0.0.1" + defaultWebServerDebug = false ) // config defines the configuration options for the VSP. type config struct { - Listen string `long:"listen" ini-name:"listen" description:"The ip:port to listen for API requests."` - LogLevel string `long:"loglevel" ini-name:"loglevel" description:"Logging level." choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error" choice:"critical"` - Network string `long:"network" ini-name:"network" description:"Decred network to use." choice:"testnet" choice:"mainnet" choice:"simnet"` - FeeXPub string `long:"feexpub" ini-name:"feexpub" description:"Cold wallet xpub used for collecting fees."` - VSPFee float64 `long:"vspfee" ini-name:"vspfee" description:"Fee percentage charged for VSP use. eg. 0.01 (1%), 0.05 (5%)."` - HomeDir string `long:"homedir" ini-name:"homedir" no-ini:"true" description:"Path to application home directory. Used for storing VSP database and logs."` - ConfigFile string `long:"configfile" ini-name:"configfile" no-ini:"true" description:"Path to configuration file."` - WalletHost string `long:"wallethost" ini-name:"wallethost" description:"The ip:port to establish a JSON-RPC connection with dcrwallet."` - WalletUser string `long:"walletuser" ini-name:"walletuser" description:"Username for dcrwallet RPC connections."` - WalletPass string `long:"walletpass" ini-name:"walletpass" description:"Password for dcrwallet RPC connections."` - WalletCert string `long:"walletcert" ini-name:"walletcert" description:"The dcrwallet RPC certificate file."` + Listen string `long:"listen" ini-name:"listen" description:"The ip:port to listen for API requests."` + LogLevel string `long:"loglevel" ini-name:"loglevel" description:"Logging level." choice:"trace" choice:"debug" choice:"info" choice:"warn" choice:"error" choice:"critical"` + Network string `long:"network" ini-name:"network" description:"Decred network to use." choice:"testnet" choice:"mainnet" choice:"simnet"` + FeeXPub string `long:"feexpub" ini-name:"feexpub" description:"Cold wallet xpub used for collecting fees."` + VSPFee float64 `long:"vspfee" ini-name:"vspfee" description:"Fee percentage charged for VSP use. eg. 0.01 (1%), 0.05 (5%)."` + HomeDir string `long:"homedir" ini-name:"homedir" no-ini:"true" description:"Path to application home directory. Used for storing VSP database and logs."` + ConfigFile string `long:"configfile" ini-name:"configfile" no-ini:"true" description:"Path to configuration file."` + WalletHost string `long:"wallethost" ini-name:"wallethost" description:"The ip:port to establish a JSON-RPC connection with dcrwallet."` + WalletUser string `long:"walletuser" ini-name:"walletuser" description:"Username for dcrwallet RPC connections."` + WalletPass string `long:"walletpass" ini-name:"walletpass" description:"Password for dcrwallet RPC connections."` + WalletCert string `long:"walletcert" ini-name:"walletcert" description:"The dcrwallet RPC certificate file."` + WebServerDebug bool `long:"webserverdebug" ini-name:"webserverdebug" description:"Enable web server debug mode (verbose logging to terminal and live-reloading templates)."` dbPath string netParams *netParams @@ -134,13 +136,14 @@ func loadConfig() (*config, error) { // Default config. cfg := config{ - Listen: defaultListen, - LogLevel: defaultLogLevel, - Network: defaultNetwork, - VSPFee: defaultVSPFee, - HomeDir: defaultHomeDir, - ConfigFile: defaultConfigFile, - WalletHost: defaultWalletHost, + Listen: defaultListen, + LogLevel: defaultLogLevel, + Network: defaultNetwork, + VSPFee: defaultVSPFee, + HomeDir: defaultHomeDir, + ConfigFile: defaultConfigFile, + WalletHost: defaultWalletHost, + WebServerDebug: defaultWebServerDebug, } // Pre-parse the command line options to see if an alternative config diff --git a/main.go b/main.go index 71abd13..869df9c 100644 --- a/main.go +++ b/main.go @@ -91,10 +91,7 @@ func run(ctx context.Context) error { NetParams: cfg.netParams.Params, FeeAccountName: feeAccountName, } - // TODO: Make releaseMode properly configurable. Release mode enables very - // detailed webserver logging and live reloading of HTML templates. - releaseMode := true - err = webapi.Start(ctx, shutdownRequestChannel, &shutdownWg, cfg.Listen, db, walletRPC, releaseMode, apiCfg) + err = webapi.Start(ctx, shutdownRequestChannel, &shutdownWg, cfg.Listen, db, walletRPC, cfg.WebServerDebug, apiCfg) if err != nil { log.Errorf("Failed to initialise webapi: %v", err) requestShutdown() diff --git a/webapi/server.go b/webapi/server.go index 22810f6..6c96123 100644 --- a/webapi/server.go +++ b/webapi/server.go @@ -28,7 +28,7 @@ var db *database.VspDatabase var walletRPC rpc.Client func Start(ctx context.Context, requestShutdownChan chan struct{}, shutdownWg *sync.WaitGroup, - listen string, vdb *database.VspDatabase, wRPC rpc.Client, releaseMode bool, config Config) error { + listen string, vdb *database.VspDatabase, wRPC rpc.Client, debugMode bool, config Config) error { // Create TCP listener. var listenConfig net.ListenConfig @@ -39,7 +39,7 @@ func Start(ctx context.Context, requestShutdownChan chan struct{}, shutdownWg *s log.Infof("Listening on %s", listen) srv := http.Server{ - Handler: router(releaseMode), + Handler: router(debugMode), ReadTimeout: 5 * time.Second, // slow requests should not hold connections opened WriteTimeout: 60 * time.Second, // hung responses must die } @@ -81,10 +81,10 @@ func Start(ctx context.Context, requestShutdownChan chan struct{}, shutdownWg *s return nil } -func router(releaseMode bool) *gin.Engine { +func router(debugMode bool) *gin.Engine { // With release mode enabled, gin will only read template files once and cache them. // With release mode disabled, templates will be reloaded on the fly. - if releaseMode { + if !debugMode { gin.SetMode(gin.ReleaseMode) } @@ -96,7 +96,7 @@ func router(releaseMode bool) *gin.Engine { // sending no response at all. router.Use(gin.Recovery()) - if !releaseMode { + if debugMode { // Logger middleware outputs very detailed logging of webserver requests // to the terminal. Does not get logged to file. router.Use(gin.Logger())