diff --git a/app/cli/src/main/scala/org/bitcoins/cli/ConsoleCli.scala b/app/cli/src/main/scala/org/bitcoins/cli/ConsoleCli.scala index 8d195f928134..3ea10fab88cd 100644 --- a/app/cli/src/main/scala/org/bitcoins/cli/ConsoleCli.scala +++ b/app/cli/src/main/scala/org/bitcoins/cli/ConsoleCli.scala @@ -30,6 +30,9 @@ object ConsoleCli { opt[Unit]("debug") .action((_, conf) => conf.copy(debug = true)) .text("Print debugging information"), + opt[Int]("rpcport") + .action((port,conf) => conf.copy(rpcPort = port)) + .text(s"The port to send our rpc request to on the server"), help('h', "help").text("Display this help message and exit"), note(sys.props("line.separator") + "Commands:"), note(sys.props("line.separator") + "===Blockchain ==="), @@ -310,7 +313,7 @@ object ConsoleCli { })) ), checkConfig { - case Config(NoCommand, _, _) => + case Config(NoCommand, _, _, _) => failure("You need to provide a command!") case _ => success } @@ -323,15 +326,15 @@ object ConsoleCli { case Some(conf) => conf } - exec(config.command, config.debug) + exec(config.command, config) } - def exec(command: CliCommand, debugEnabled: Boolean = false): Try[String] = { + def exec(command: CliCommand, config: Config): Try[String] = { import System.err.{println => printerr} /** Prints the given message to stderr if debug is set */ def debug(message: Any): Unit = { - if (debugEnabled) { + if (config.debug) { printerr(s"DEBUG: $message") } } @@ -413,7 +416,7 @@ object ConsoleCli { HttpURLConnectionBackend() val request = sttp - .post(uri"http://$host:$port/") + .post(uri"http://$host:${config.rpcPort}/") .contentType("application/json") .body({ val uuid = java.util.UUID.randomUUID.toString @@ -468,8 +471,6 @@ object ConsoleCli { }.flatten } - // TODO make this dynamic - def port = 9999 def host = "localhost" case class RequestParam( @@ -485,9 +486,14 @@ object ConsoleCli { case class Config( command: CliCommand = CliCommand.NoCommand, network: Option[NetworkParameters] = None, - debug: Boolean = false + debug: Boolean = false, + rpcPort: Int = 9999 ) +object Config { + val empty = Config() +} + sealed abstract class CliCommand object CliCommand { diff --git a/app/gui/src/main/scala/org/bitcoins/gui/WalletGUIModel.scala b/app/gui/src/main/scala/org/bitcoins/gui/WalletGUIModel.scala index ae8e9f32d10f..639372b620e9 100644 --- a/app/gui/src/main/scala/org/bitcoins/gui/WalletGUIModel.scala +++ b/app/gui/src/main/scala/org/bitcoins/gui/WalletGUIModel.scala @@ -1,7 +1,7 @@ package org.bitcoins.gui import org.bitcoins.cli.CliCommand.{GetBalance, GetNewAddress, SendToAddress} -import org.bitcoins.cli.ConsoleCli +import org.bitcoins.cli.{Config, ConsoleCli} import org.bitcoins.core.currency.Bitcoins import org.bitcoins.core.protocol.BitcoinAddress import org.bitcoins.gui.dialog.{GetNewAddressDialog, SendDialog} @@ -28,7 +28,7 @@ class WalletGUIModel() { taskRunner.run( caption = "Get New Address", op = { - ConsoleCli.exec(GetNewAddress) match { + ConsoleCli.exec(GetNewAddress,Config.empty) match { case Success(commandReturn) => address.value = commandReturn case Failure(err) => throw err } @@ -49,7 +49,8 @@ class WalletGUIModel() { ConsoleCli.exec( SendToAddress(BitcoinAddress(address).get, Bitcoins(BigDecimal(amount)), - satoshisPerVirtualByte = None)) match { + satoshisPerVirtualByte = None), + Config.empty) match { case Success(txid) => GlobalData.log.value = s"Sent $amount to $address in tx: $txid\n\n${GlobalData.log()}" @@ -64,7 +65,7 @@ class WalletGUIModel() { } private def updateBalance(): Unit = { - ConsoleCli.exec(GetBalance(isSats = false)) match { + ConsoleCli.exec(GetBalance(isSats = false), Config.empty) match { case Success(commandReturn) => GlobalData.currentBalance.value = commandReturn.split(' ').head.toDouble case Failure(err) => diff --git a/app/server/src/main/resources/reference.conf b/app/server/src/main/resources/reference.conf index 720e64ca3040..b88c8377dd5b 100644 --- a/app/server/src/main/resources/reference.conf +++ b/app/server/src/main/resources/reference.conf @@ -78,6 +78,11 @@ bitcoin-s { # before we timeout addressQueueTimeout = 5 seconds } + + server { + # The port we bind our rpc server on + rpcport = 9999 + } } akka { diff --git a/app/server/src/main/scala/org/bitcoins/server/BitcoinSAppConfig.scala b/app/server/src/main/scala/org/bitcoins/server/BitcoinSAppConfig.scala index 67f4e0cc7d48..0e95388a8115 100644 --- a/app/server/src/main/scala/org/bitcoins/server/BitcoinSAppConfig.scala +++ b/app/server/src/main/scala/org/bitcoins/server/BitcoinSAppConfig.scala @@ -44,6 +44,18 @@ case class BitcoinSAppConfig( // be equal nodeConf.config } + + def serverConf: Config = { + config.getConfig("server") + } + + def rpcPortOpt: Option[Int] = { + if(serverConf.hasPath("rpcport")) { + Some(serverConf.getInt("rpcport")) + } else { + None + } + } } /** diff --git a/app/server/src/main/scala/org/bitcoins/server/Main.scala b/app/server/src/main/scala/org/bitcoins/server/Main.scala index 27eb410e9b37..e37e8fca791b 100644 --- a/app/server/src/main/scala/org/bitcoins/server/Main.scala +++ b/app/server/src/main/scala/org/bitcoins/server/Main.scala @@ -26,11 +26,13 @@ import scala.concurrent.{Await, ExecutionContext, Future} object Main extends App { implicit val system = ActorSystem("bitcoin-s") implicit val ec: ExecutionContext = system.dispatcher + val argsWithIndex = args.zipWithIndex implicit val conf = { - val dataDirIndexOpt = args.zipWithIndex - .find(_._1.toLowerCase == "--datadir") + val dataDirIndexOpt = { + argsWithIndex.find(_._1.toLowerCase == "--datadir") + } val datadirPath = dataDirIndexOpt match { case None => AppConfig.DEFAULT_BITCOIN_S_DATADIR case Some((_, dataDirIndex)) => @@ -39,7 +41,12 @@ object Main extends App { } BitcoinSAppConfig(datadirPath) } - + val rpcPortOpt: Option[Int] = { + val portOpt = argsWithIndex.find(_._1.toLowerCase == "--rpcport") + portOpt.map { + case (_,idx) => args(idx+1).toInt + } + } private val logger = HttpLoggerImpl(conf.nodeConf).getLogger implicit val walletConf: WalletAppConfig = conf.walletConf @@ -75,9 +82,17 @@ object Main extends App { val nodeRoutes = NodeRoutes(node) val chainRoutes = ChainRoutes(chainApi) val coreRoutes = CoreRoutes(Core) - val server = - Server(nodeConf, Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes)) - + val server = rpcPortOpt match { + case Some(rpcport) => + Server(nodeConf, Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes), rpcport = rpcport) + case None => + conf.rpcPortOpt match { + case Some(rpcport) => + Server(nodeConf, Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes), rpcport) + case None => + Server(nodeConf, Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes)) + } + } server.start() } } yield { diff --git a/app/server/src/main/scala/org/bitcoins/server/Server.scala b/app/server/src/main/scala/org/bitcoins/server/Server.scala index 46590cc7248f..d7af9f517fe0 100644 --- a/app/server/src/main/scala/org/bitcoins/server/Server.scala +++ b/app/server/src/main/scala/org/bitcoins/server/Server.scala @@ -7,13 +7,14 @@ import akka.stream.ActorMaterializer import akka.http.scaladsl.model._ import akka.http.scaladsl.server._ import akka.http.scaladsl.server.Directives._ - import de.heikoseeberger.akkahttpupickle.UpickleSupport._ import akka.http.scaladsl.server.directives.DebuggingDirectives import akka.event.Logging import org.bitcoins.db.AppConfig -case class Server(conf: AppConfig, handlers: Seq[ServerRoute])( +import scala.concurrent.Future + +case class Server(conf: AppConfig, handlers: Seq[ServerRoute], rpcport: Int = 9999)( implicit system: ActorSystem) extends HttpLogger { implicit private val config: AppConfig = conf @@ -76,9 +77,9 @@ case class Server(conf: AppConfig, handlers: Seq[ServerRoute])( } } - def start() = { + def start(): Future[Http.ServerBinding] = { val httpFut = - Http().bindAndHandle(route, "localhost", 9999) + Http().bindAndHandle(route, "localhost", rpcport) httpFut.foreach { http => logger.info(s"Started Bitcoin-S HTTP server at ${http.localAddress}") } diff --git a/docs/applications/server.md b/docs/applications/server.md index 2ba0c50da7f3..3ca18c64eadd 100644 --- a/docs/applications/server.md +++ b/docs/applications/server.md @@ -36,6 +36,13 @@ If you would like to pass in a custom datadir for your server, you can do ```bash ./app/server/target/universal/stage/bin/bitcoin-s-server --datadir /path/to/datadir/ ``` + +You can also pass in a custom `rpcport` to bind to + +```bash +./app/server/target/universal/stage/bin/bitcoin-s-server --rpcport 12345 +``` + For more information on configuring the server please see our [configuration](../config/configuration.md) document For more information on how to use our built in `cli` to interact with the server please see [cli.md](cli.md) \ No newline at end of file diff --git a/docs/config/configuration.md b/docs/config/configuration.md index 3a9ff0ba3223..567fc2eb1418 100644 --- a/docs/config/configuration.md +++ b/docs/config/configuration.md @@ -153,6 +153,11 @@ bitcoin-s { addressQueueTimeout = 5 seconds } + + server { + # The port we bind our rpc server on + rpcport = 9999 + } } diff --git a/testkit/src/main/resources/reference.conf b/testkit/src/main/resources/reference.conf index 6767e313b8ba..bb5fa6992617 100644 --- a/testkit/src/main/resources/reference.conf +++ b/testkit/src/main/resources/reference.conf @@ -80,6 +80,11 @@ bitcoin-s { # before we timeout addressQueueTimeout = 5 seconds } + + server { + # The port we bind our rpc server on + rpcport = 9999 + } }