Last active
July 26, 2016 12:37
-
-
Save JoolsF/33d46488bdbd0779bbb2e55ded216d98 to your computer and use it in GitHub Desktop.
Akka HTTP basic setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.http.scaladsl.model.{HttpEntity, HttpResponse} | |
import akka.http.scaladsl.server.Directives._ | |
import akka.http.scaladsl.Http | |
import akka.stream.ActorMaterializer | |
import akka.actor.ActorSystem | |
/** | |
* Basic akka http setup | |
* Test with | |
* curl localhost:9000/ping | |
* curl localhost:9000/subpath1/subpath2?batch_size=3 | |
* curl --data "julian" localhost:9000/subpath1/subpath2 | |
* | |
*/ | |
class RestServer(implicit val system: ActorSystem, implicit val materializer: ActorMaterializer) extends Routes { | |
def startServer(address: String, port: Int) = { | |
Http().bindAndHandle(routes, address, port) | |
println("\nService running") | |
} | |
} | |
object RestServer extends App { | |
implicit val system: ActorSystem = ActorSystem() | |
implicit val materializer = ActorMaterializer() | |
val server = new RestServer() | |
server.startServer("localhost", 9000) | |
} | |
trait Routes { | |
val routes = { | |
path("ping"){ | |
get { | |
complete { | |
HttpResponse(entity = HttpEntity(java.util.Calendar.getInstance().getTime.toString)) | |
} | |
} | |
} ~ pathPrefix("subpath1") { | |
path("subpath2"){ | |
get { | |
parameters('batch_size){ batch_size => | |
complete { | |
HttpResponse(entity = HttpEntity(s"Placeholder get response with batch_size $batch_size\n")) | |
} | |
} | |
} | |
} ~ { | |
post{ | |
entity(as[String]){ str => | |
complete { | |
HttpResponse(entity = HttpEntity(s"Place holder post response. Echo $str body\n")) | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment