Last active
May 26, 2021 23:12
-
-
Save michalkowol/fd8132a4233a8d4587ee to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.michalkowol | |
import java.util.concurrent.TimeUnit | |
import akka.actor.{Props} | |
import com.paypal.cascade.akka.actor.{ServiceActor, ActorSystemWrapper} | |
import com.paypal.cascade.common.app.CascadeApp | |
import com.paypal.cascade.http.actor.SprayActor | |
import com.paypal.cascade.http.resource.ResourceDriver | |
import com.paypal.cascade.http.server.SprayConfiguration | |
import spray.http.HttpHeaders.Location | |
import spray.routing.Directives._ | |
import akka.util.Timeout | |
import com.paypal.cascade.http.resource.AbstractResourceActor | |
import com.paypal.cascade.http.resource.HttpResourceActor.{ProcessRequest, ResourceContext} | |
import spray.http.{HttpResponse, StatusCodes, HttpRequest} | |
import scala.concurrent.duration._ | |
import scala.util.Try | |
case class User(username: Option[String]) | |
case class UserWithId(id: Int, username: String) | |
class MyActor extends ServiceActor { | |
override def receive: Receive = { | |
case UserWithId(id, username) => sender() ! s"hello $username with id $id!" | |
} | |
} | |
class MyHttpResource(ctx: ResourceContext) extends AbstractResourceActor(ctx) { | |
private val myActor = context.actorOf(Props[MyActor], "myActor") | |
override def resourceReceive: Receive = { | |
case ProcessRequest(User(Some(username))) => completeToJSON(StatusCodes.OK, s"hello $username!") | |
case ProcessRequest(User(None)) => completeToJSON(StatusCodes.OK, "hello anonymous!") | |
case ProcessRequest(req: UserWithId) => myActor ! req | |
case msg: String => | |
println(ctx.reqContext.request) | |
//completeToJSON(StatusCodes.OK, msg) | |
complete(HttpResponse(StatusCodes.OK).withEntity(msg).withHeaders(Location("/"))) | |
} | |
} | |
object MyHttpResource { | |
def apply(ctx: ResourceContext): MyHttpResource = { | |
new MyHttpResource(ctx) | |
} | |
def create(ctx: ResourceContext): MyHttpResource = apply(ctx) | |
def username(req: HttpRequest): Try[User] = Try { | |
val cookies = req.cookies | |
val user = cookies.find(cookie => cookie.name == "user").map(cookie => cookie.content) | |
User(user) | |
} | |
def usernameAndId(userId: Int)(req: HttpRequest): Try[UserWithId] = { | |
username(req).map { user => | |
UserWithId(userId, user.username.getOrElse("michal")) | |
} | |
} | |
} | |
object MyHttpServer extends CascadeApp { | |
val systemWrapper = new ActorSystemWrapper("MyHttpService") | |
val TimeoutSecs = 10 | |
val Port = 8080 | |
val Backlog = 5 | |
private implicit val actorRefFactory = systemWrapper.actorRefFactory | |
private implicit val timeout = new Timeout(TimeoutSecs, TimeUnit.SECONDS) | |
val config = SprayConfiguration("my-http-server", Port, Backlog) { | |
get { | |
path("hello") { | |
ResourceDriver.serve(MyHttpResource.create, MyHttpResource.username) | |
} ~ path("hello" / IntNumber) { id => | |
ResourceDriver.serve(MyHttpResource.create, MyHttpResource.usernameAndId(id), resourceTimeout = 30.seconds) | |
} | |
} | |
} | |
SprayActor.start(systemWrapper, config) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment