diff --git a/eclair-node-gui/src/main/scala/fr/acinq/eclair/JavafxBoot.scala b/eclair-node-gui/src/main/scala/fr/acinq/eclair/JavafxBoot.scala index c38040b47f..9152e82cad 100644 --- a/eclair-node-gui/src/main/scala/fr/acinq/eclair/JavafxBoot.scala +++ b/eclair-node-gui/src/main/scala/fr/acinq/eclair/JavafxBoot.scala @@ -22,7 +22,7 @@ import akka.actor.ActorSystem import fr.acinq.eclair.gui.{FxApp, FxPreloader} import grizzled.slf4j.Logging import javafx.application.Application - +import scala.concurrent.ExecutionContext.Implicits.global /** * Created by PM on 25/01/2016. */ @@ -33,7 +33,10 @@ object JavafxBoot extends App with Logging { if (headless) { implicit val system = ActorSystem("eclair-node-gui") - new Setup(datadir).bootstrap + val setup = new Setup(datadir) + setup.bootstrap.map { kit => + Boot.startApiServiceIfEnabled(setup.config, kit) + } } else { System.setProperty("javafx.preloader", classOf[FxPreloader].getName) Application.launch(classOf[FxApp], datadir.getAbsolutePath) diff --git a/eclair-node-gui/src/main/scala/fr/acinq/eclair/gui/FxApp.scala b/eclair-node-gui/src/main/scala/fr/acinq/eclair/gui/FxApp.scala index 30f4f1df73..163d8c14a9 100644 --- a/eclair-node-gui/src/main/scala/fr/acinq/eclair/gui/FxApp.scala +++ b/eclair-node-gui/src/main/scala/fr/acinq/eclair/gui/FxApp.scala @@ -104,7 +104,8 @@ class FxApp extends Application with Logging { system.eventStream.subscribe(guiUpdater, classOf[ElectrumEvent]) pKit.completeWith(setup.bootstrap) pKit.future.onComplete { - case Success(_) => + case Success(kit) => + Boot.startApiServiceIfEnabled(setup.config, kit) Platform.runLater(new Runnable { override def run(): Unit = { val scene = new Scene(mainRoot) diff --git a/eclair-node/src/main/scala/fr/acinq/eclair/Boot.scala b/eclair-node/src/main/scala/fr/acinq/eclair/Boot.scala index 543f3af5e4..45ad4bde24 100644 --- a/eclair-node/src/main/scala/fr/acinq/eclair/Boot.scala +++ b/eclair-node/src/main/scala/fr/acinq/eclair/Boot.scala @@ -21,10 +21,9 @@ import java.io.File import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.stream.{ActorMaterializer, BindFailedException} -import com.typesafe.config.ConfigFactory +import com.typesafe.config.Config import fr.acinq.eclair.api.Service import grizzled.slf4j.Logging - import scala.concurrent.{Await, ExecutionContext} import scala.util.{Failure, Success} @@ -44,34 +43,44 @@ object Boot extends App with Logging { plugins.foreach(_.onSetup(setup)) setup.bootstrap onComplete { case Success(kit) => + startApiServiceIfEnabled(setup.config, kit) plugins.foreach(_.onKit(kit)) - val config = setup.config - if(config.getBoolean("api.enabled")){ - logger.info(s"json API enabled on port=${config.getInt("api.port")}") - implicit val materializer = ActorMaterializer() - val apiPassword = config.getString("api.password") match { - case "" => throw EmptyAPIPasswordException - case valid => valid - } - val apiRoute = new Service { - override val actorSystem = system - override val mat = materializer - override val password = apiPassword - override val eclairApi: Eclair = new EclairImpl(kit) - }.route - Http().bindAndHandle(apiRoute, config.getString("api.binding-ip"), config.getInt("api.port")).onFailure { - case _: BindFailedException => onError(TCPBindException(config.getInt("api.port"))) - } - } else { - logger.info("json API disabled") - } - case Failure(t) => onError(t) } } catch { case t: Throwable => onError(t) } + /** + * Starts the http APIs service if enabled in the configuration + * + * @param config + * @param kit + * @param system + * @param ec + */ + def startApiServiceIfEnabled(config: Config, kit: Kit)(implicit system: ActorSystem, ec: ExecutionContext) = { + if(config.getBoolean("api.enabled")){ + logger.info(s"json API enabled on port=${config.getInt("api.port")}") + implicit val materializer = ActorMaterializer() + val apiPassword = config.getString("api.password") match { + case "" => throw EmptyAPIPasswordException + case valid => valid + } + val apiRoute = new Service { + override val actorSystem = system + override val mat = materializer + override val password = apiPassword + override val eclairApi: Eclair = new EclairImpl(kit) + }.route + Http().bindAndHandle(apiRoute, config.getString("api.binding-ip"), config.getInt("api.port")).onFailure { + case _: BindFailedException => onError(TCPBindException(config.getInt("api.port"))) + } + } else { + logger.info("json API disabled") + } + } + def onError(t: Throwable): Unit = { val errorMsg = if (t.getMessage != null) t.getMessage else t.getClass.getSimpleName System.err.println(s"fatal error: $errorMsg")