diff --git a/src/main/scala/com/fluency03/blockchain/api/Server.scala b/src/main/scala/com/fluency03/blockchain/api/Server.scala index a8520e2..b1cc688 100644 --- a/src/main/scala/com/fluency03/blockchain/api/Server.scala +++ b/src/main/scala/com/fluency03/blockchain/api/Server.scala @@ -7,14 +7,14 @@ import akka.http.scaladsl.Http.ServerBinding import akka.http.scaladsl.server.Directives._ import akka.http.scaladsl.server.Route import akka.stream.ActorMaterializer -import com.fluency03.blockchain.api.actors.{BlockActor, BlockchainActor, TransactionActor} -import com.fluency03.blockchain.api.routes.{BlockRoutes, BlockchainRoutes, TransactionRoutes} +import com.fluency03.blockchain.api.actors._ +import com.fluency03.blockchain.api.routes._ import com.typesafe.config.ConfigFactory import scala.concurrent.{Await, ExecutionContextExecutor, Future} import scala.concurrent.duration.Duration -object Server extends App with BlockchainRoutes with BlockRoutes with TransactionRoutes { +object Server extends App with BlockchainRoutes with BlockRoutes with TransactionRoutes with GenericRoutes { implicit val system: ActorSystem = ActorSystem("blockchain-http-service") implicit val materializer: ActorMaterializer = ActorMaterializer() implicit val executionContext: ExecutionContextExecutor = system.dispatcher @@ -29,7 +29,7 @@ object Server extends App with BlockchainRoutes with BlockRoutes with Transactio val blockActor: ActorRef = system.actorOf(BlockActor.props, BLOCK_ACTOR_NAME) val txActor: ActorRef = system.actorOf(TransactionActor.props, TX_ACTOR_NAME) - lazy val routes: Route = blockchainRoutes ~ blockRoutes ~ txRoutes + lazy val routes: Route = blockchainRoutes ~ blockRoutes ~ txRoutes ~ genericRoutes val bindingFuture: Future[ServerBinding] = Http().bindAndHandle(routes, host, port) diff --git a/src/main/scala/com/fluency03/blockchain/api/routes/GenericRoutes.scala b/src/main/scala/com/fluency03/blockchain/api/routes/GenericRoutes.scala new file mode 100644 index 0000000..2160365 --- /dev/null +++ b/src/main/scala/com/fluency03/blockchain/api/routes/GenericRoutes.scala @@ -0,0 +1,34 @@ +package com.fluency03.blockchain.api.routes + +import akka.event.Logging +import akka.http.scaladsl.model.StatusCodes +import akka.http.scaladsl.server.Directives._ +import akka.http.scaladsl.server.Route +import akka.http.scaladsl.server.directives.MethodDirectives.{delete, get, post} +import akka.http.scaladsl.server.directives.RouteDirectives.complete +import com.fluency03.blockchain.Util._ + +case class Input(data: String) + +trait GenericRoutes extends Routes { + lazy val log = Logging(system, classOf[GenericRoutes]) + + lazy val genericRoutes: Route = + pathPrefix("generic") { + path("hash-of-string") { + post { + entity(as[Input]) { in => complete((StatusCodes.Created, hashOf(in.data))) } + } + } ~ + path("base64-of-string") { + post { + entity(as[Input]) { in => complete((StatusCodes.Created, toBase64(in.data))) } + } + } ~ + path("string-of-base64") { + post { + entity(as[Input]) { in => complete((StatusCodes.Created, fromBase64(in.data))) } + } + } + } +}