Config for web server release/debug mode (#40)
This commit is contained in:
parent
36c748ba12
commit
72fc9afa40
39
config.go
39
config.go
@ -25,21 +25,23 @@ var (
|
|||||||
defaultConfigFilename = "dcrvsp.conf"
|
defaultConfigFilename = "dcrvsp.conf"
|
||||||
defaultConfigFile = filepath.Join(defaultHomeDir, defaultConfigFilename)
|
defaultConfigFile = filepath.Join(defaultHomeDir, defaultConfigFilename)
|
||||||
defaultWalletHost = "127.0.0.1"
|
defaultWalletHost = "127.0.0.1"
|
||||||
|
defaultWebServerDebug = false
|
||||||
)
|
)
|
||||||
|
|
||||||
// config defines the configuration options for the VSP.
|
// config defines the configuration options for the VSP.
|
||||||
type config struct {
|
type config struct {
|
||||||
Listen string `long:"listen" ini-name:"listen" description:"The ip:port to listen for API requests."`
|
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"`
|
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"`
|
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."`
|
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%)."`
|
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."`
|
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."`
|
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."`
|
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."`
|
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."`
|
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."`
|
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
|
dbPath string
|
||||||
netParams *netParams
|
netParams *netParams
|
||||||
@ -134,13 +136,14 @@ func loadConfig() (*config, error) {
|
|||||||
|
|
||||||
// Default config.
|
// Default config.
|
||||||
cfg := config{
|
cfg := config{
|
||||||
Listen: defaultListen,
|
Listen: defaultListen,
|
||||||
LogLevel: defaultLogLevel,
|
LogLevel: defaultLogLevel,
|
||||||
Network: defaultNetwork,
|
Network: defaultNetwork,
|
||||||
VSPFee: defaultVSPFee,
|
VSPFee: defaultVSPFee,
|
||||||
HomeDir: defaultHomeDir,
|
HomeDir: defaultHomeDir,
|
||||||
ConfigFile: defaultConfigFile,
|
ConfigFile: defaultConfigFile,
|
||||||
WalletHost: defaultWalletHost,
|
WalletHost: defaultWalletHost,
|
||||||
|
WebServerDebug: defaultWebServerDebug,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pre-parse the command line options to see if an alternative config
|
// Pre-parse the command line options to see if an alternative config
|
||||||
|
|||||||
5
main.go
5
main.go
@ -91,10 +91,7 @@ func run(ctx context.Context) error {
|
|||||||
NetParams: cfg.netParams.Params,
|
NetParams: cfg.netParams.Params,
|
||||||
FeeAccountName: feeAccountName,
|
FeeAccountName: feeAccountName,
|
||||||
}
|
}
|
||||||
// TODO: Make releaseMode properly configurable. Release mode enables very
|
err = webapi.Start(ctx, shutdownRequestChannel, &shutdownWg, cfg.Listen, db, walletRPC, cfg.WebServerDebug, apiCfg)
|
||||||
// detailed webserver logging and live reloading of HTML templates.
|
|
||||||
releaseMode := true
|
|
||||||
err = webapi.Start(ctx, shutdownRequestChannel, &shutdownWg, cfg.Listen, db, walletRPC, releaseMode, apiCfg)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to initialise webapi: %v", err)
|
log.Errorf("Failed to initialise webapi: %v", err)
|
||||||
requestShutdown()
|
requestShutdown()
|
||||||
|
|||||||
@ -28,7 +28,7 @@ var db *database.VspDatabase
|
|||||||
var walletRPC rpc.Client
|
var walletRPC rpc.Client
|
||||||
|
|
||||||
func Start(ctx context.Context, requestShutdownChan chan struct{}, shutdownWg *sync.WaitGroup,
|
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.
|
// Create TCP listener.
|
||||||
var listenConfig net.ListenConfig
|
var listenConfig net.ListenConfig
|
||||||
@ -39,7 +39,7 @@ func Start(ctx context.Context, requestShutdownChan chan struct{}, shutdownWg *s
|
|||||||
log.Infof("Listening on %s", listen)
|
log.Infof("Listening on %s", listen)
|
||||||
|
|
||||||
srv := http.Server{
|
srv := http.Server{
|
||||||
Handler: router(releaseMode),
|
Handler: router(debugMode),
|
||||||
ReadTimeout: 5 * time.Second, // slow requests should not hold connections opened
|
ReadTimeout: 5 * time.Second, // slow requests should not hold connections opened
|
||||||
WriteTimeout: 60 * time.Second, // hung responses must die
|
WriteTimeout: 60 * time.Second, // hung responses must die
|
||||||
}
|
}
|
||||||
@ -81,10 +81,10 @@ func Start(ctx context.Context, requestShutdownChan chan struct{}, shutdownWg *s
|
|||||||
return nil
|
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 enabled, gin will only read template files once and cache them.
|
||||||
// With release mode disabled, templates will be reloaded on the fly.
|
// With release mode disabled, templates will be reloaded on the fly.
|
||||||
if releaseMode {
|
if !debugMode {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ func router(releaseMode bool) *gin.Engine {
|
|||||||
// sending no response at all.
|
// sending no response at all.
|
||||||
router.Use(gin.Recovery())
|
router.Use(gin.Recovery())
|
||||||
|
|
||||||
if !releaseMode {
|
if debugMode {
|
||||||
// Logger middleware outputs very detailed logging of webserver requests
|
// Logger middleware outputs very detailed logging of webserver requests
|
||||||
// to the terminal. Does not get logged to file.
|
// to the terminal. Does not get logged to file.
|
||||||
router.Use(gin.Logger())
|
router.Use(gin.Logger())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user