Advertisement
Guest User

Untitled

a guest
Jan 12th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.88 KB | None | 0 0
  1. import akka.actor.{ActorSystem, Props}
  2. import akka.event.Logging
  3. import akka.io.IO
  4. import akka.pattern.ask
  5. import akka.util.Timeout
  6. import spray.can.Http
  7. import spray.http.{HttpRequest, HttpResponse, StatusCodes}
  8. import spray.routing._
  9. import spray.routing.directives.{DebuggingDirectives, LogEntry}
  10.  
  11. import scala.concurrent.duration._
  12.  
  13. //noinspection ScalaStyle
  14. object hello extends App {
  15.   implicit val system = ActorSystem("my-system")
  16.   implicit val timeout: Timeout = Timeout(20.seconds)
  17.  
  18.   def requestMethodAndResponseStatusAsInfo(req: HttpRequest): Any => Option[LogEntry] = {
  19.     case res: HttpResponse =>
  20.       Some(LogEntry(req.method + ":" + res.message.status, Logging.InfoLevel))
  21.     case _ =>
  22.       None // other kind of responses
  23.   }
  24.  
  25.   var wrapper = DebuggingDirectives.logRequestResponse(requestMethodAndResponseStatusAsInfo _)
  26.  
  27.   IO(Http)(system) ask Http.Bind(
  28.     listener = system.actorOf(
  29.       Props(
  30.         new MyService()
  31.       ),
  32.       "root"
  33.     ),
  34.     interface = "0.0.0.0",
  35.     port = 8080
  36.   )
  37.  
  38.   class MyService extends HttpServiceActor {
  39.  
  40.     implicit val specificExceptionHandler = ExceptionHandler {
  41.       case e: NoSuchElementException =>
  42.         ctx => ctx.complete(StatusCodes.NotFound)
  43.       case e: IllegalArgumentException =>
  44.         ctx => ctx.complete(StatusCodes.BadRequest)
  45.       case e: UnsupportedOperationException =>
  46.         ctx => ctx.complete(StatusCodes.NotImplemented)
  47.       case other =>
  48.         ctx => {
  49.           println("count 500")
  50.           ctx.complete(StatusCodes.InternalServerError)
  51.         }
  52.     }
  53.  
  54.     implicit val rs: RoutingSettings = RoutingSettings.default(system)
  55.  
  56.     def receive = runRoute {
  57.       wrapper {
  58.         path("hello") {
  59.           get {
  60.             complete {
  61.               throw new IllegalStateException("hello ")
  62.             }
  63.           }
  64.         }
  65.       }
  66.     }
  67.   }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement