Skip to content

Commit

Permalink
add GenericRoutes
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 16, 2018
1 parent b52f6e1 commit cec57cc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/scala/com/fluency03/blockchain/api/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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))) }
}
}
}
}

0 comments on commit cec57cc

Please sign in to comment.