Skip to content

Commit

Permalink
Allow http APIs to be enabled with using the GUI as well
Browse files Browse the repository at this point in the history
  • Loading branch information
araspitzu committed Aug 22, 2019
1 parent 3469eb5 commit e917f5b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 32 additions & 23 deletions eclair-node/src/main/scala/fr/acinq/eclair/Boot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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")
Expand Down

0 comments on commit e917f5b

Please sign in to comment.