Guest User

Untitled

a guest
Jan 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. trait HandlerArgs extends FiniteStateMachineActor {
  2. def isInitial: Boolean
  3. }
  4.  
  5. class Handler extends ((Request, HandlerArgs) => Response)
  6.  
  7. class Middleware extends (Handler => Handler)
  8.  
  9. object Server {
  10.  
  11. type ResponseWriter = Response => Result
  12. val handler: Handler
  13.  
  14. object Executor {
  15. @tailrec
  16. def apply(handle: Handler, write: ResponseWriter, onError: ErrorHandler): Response = {
  17. val res = handle(req, args)
  18. if (!args.isInitial) onError(new YouCantDoThisRightHereNowException())
  19. if (!res.isInstanceOf[EndResponse]) {
  20. write(res)
  21. apply(handle, socket)
  22. } else {
  23. res
  24. }
  25. }
  26. }
  27.  
  28. def apply(req: Request, args: HandlerArgs) = {
  29. (future { Executor((r, a) => handler(r, a), socket.send _) } onComplete (socket.send _) onException (throw _)).get
  30. }
  31. }
  32.  
  33. sealed trait Response {
  34. type ContentType
  35. def headers: Map[String, String]
  36. def content: ContentType
  37. def update(statusLine: String, headers: Map[String, String], content: ContentType): ResponsePrelude
  38. }
  39. sealed trait ResponsePrelude extends Response {
  40. def statusLine: String
  41. def update(statusLine: String, headers: Map[String, String], content: ContentType): ResponsePrelude
  42. }
  43. sealed trait ResponseCompletion { self: Response =>
  44. }
  45. sealed trait ResponsePart extends Response {
  46. def update(headers: Map[String, String], content: ContentType): ResponsePart
  47. }
  48.  
  49. case class BeginResponse[T : Manifest](statusLine: String, headers: Map[String, String], content: T) extends ResponsePrelude {
  50. type ContentType = T
  51. def update(statusLine: String = this.statusLine, headers: Map[String, String] = headers, content: ContentType = content): ResponsePrelude =
  52. copy(statusLine, headers, content)
  53. }
  54. case class ResponseChunk[T : Manifest](headers: Map[String, String], content: T) {
  55. type ContentType = T
  56. def update(headers: Map[String, String] = headers, content: ContentType = content): ResponsePrelude =
  57. copy(headers, content)
  58. }
  59. case class EndResponse[T: Manifest](headers: Map[String, String], content: T) extends ResponseCompletion {
  60. type ContentType = T
  61. def update(headers: Map[String, String] = headers, content: ContentType = content): ResponsePrelude =
  62. copy(headers, content)
  63. }
  64. case class DefaultResponse[T: Manifest](statusLine: String, headers: Map[String, String], content: T) extends ResponsePrelude with ResponseCompletion {
  65. type ContentType = T
  66. def update(statusLine: String = this.statusLine, headers: Map[String, String] = headers, content: ContentType = content): ResponsePrelude =
  67. copy(statusLine, headers, content)
  68. }
Add Comment
Please, Sign In to add comment