Unfiltered Unveiled

1,968 views
2,110 views

Published on

The slides of my introduction to Unfiltered, without the live coding session. ;-)

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,968
On SlideShare
0
From Embeds
0
Number of Embeds
753
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Unfiltered Unveiled

    1. 1. UnfilteredUnveiled THE UNFRAMEWORK
    2. 2. WhoamiCode / train Scala for a livingLately for a NYC startupUsing UnfilteredCurrently freelancerFormer XebiaFormer TomTomFormer Sun MicrostemsFormer ...
    3. 3. HTTP →GET / HTTP/1.1Host: nxt.flotsam.nl ← HTTP/1.1 200 OK Date: Mon, 29 Oct 2012 … Last-Modified: Wed, 17Accept: text/html … ETag: "4ce43eb8b40a4f…" Content-Type: text/html Content-Length: 9636 Server: AmazonS3
    4. 4. FrameworksSolve manyproblems aroundgiven domain
    5. 5. FrameworksSimple problems ☹
    6. 6. ToolkitLoosely coupledcomponents that can becomposed to solve Simple problems ☺different problems Complex problems ☺
    7. 7. HTTPStuff Stuff
    8. 8. HTTPRequest Response
    9. 9. Web Appl.Request ResponseRequest’ Response’Request’’ Response’’Request’’’ Response’’’
    10. 10. That rings a bell … match { case … => … case … => … case … => … case … => …
    11. 11. Pattern matching obj match { case i: Int => i * 2 case s: String => s + s }
    12. 12. Partial Function { case … => … case … => … case … => … case … => …
    13. 13. Exampleval duplicate: PartialFunction[Any,Any] = { case s: String => s + s case i: Int => i * 2}duplicate(3) // 6duplicate("yuk") // "yukyuk”duplicate(4.3) // MatchError
    14. 14. Unfiltered{ Intent case _ => ResponseString(“yay”)} GET / HTTP/ HTTP/1.1 200 OK Content-Length: 3
    15. 15. Hitting the road import unfiltered.filter._ import unfiltered.jetty._ import unfiltered.response._ Http(8090).filter(Planify { case _ => ResponseString("Ok") }).run
    16. 16. ResponseFunction ① case … => … case … => … ~> … ~> … Ok Html(…) NotFound Html5(…) ResponseString(…) JsonContent Json(…) TextXmlContent ResponseWriter(…) Redirect(…)
    17. 17. ResponseFunction ② Ok ~> PlainTextContent ~> ResponseString("foo") Ok→ resp.setStatus(SC_OK) PlainTextContent→ resp.setContextType("text/plain") RepsonseString("foo")→ resp.setCharacterEncoding("UTF-8") val writer = resp.getWriter() writer.print("foo") writer.close
    18. 18. ResponseFunction ③ Ok ~> PlainTextContent ~> ResponseString("foo") NotFound ~> HtmlContent ~> Html(<html> <body>Not found</body> </html>) Redirect("/index.html")
    19. 19. Scalate Templates for Scala SSP<% val decks: List[Decks] %> SCAML<html><body> Mustache<ul> …<% for (deck <- decks) { %><li><%= deck.name%></li><% } %> -@ val decks: List[Decks]</ul> %html</body> <html> %body</html> <body> %ul <ul> - for (deck <- decks) {{#decks}} %li= deck.name <li>{{name}}</li> {{/decks}}
    20. 20. SCAML In a Nutshell!!! <!DOCTYPE html PUBLIC "-//…%html <html> %body <body> .masthead <div class="masthead"/> #main <div id="main"/> %p(onclick="") YES <p onclick="">YES</p> :javascript <script language="javascript"/> :markdown *Nice* <p><em>Nice</em></p> </body> </html>
    21. 21. Extractors ① case … => … case … & … & … => …GET(…) Accept(…)POST(…) UserAgent(…)DELETE(…) Host(…)PUT(…) IfModifiedSince(…)Path(…) Referer(…)
    22. 22. Extractors ②case GET(_)case GET(Path(path))case Path(Seg("give","me",something :: Nil))(Matches "/give/me/money", setting something to "money".)case Path("/") & Params(params)case Accept("application/json")
    23. 23. Extractors ③HttpRequest[A] → { case … => … }object DotJson { unapply[A](req: HttpRequest[A]) = req.uri.endsWith(".json")}case DotJson() => ResponseString("Json!")
    24. 24. QParams ☣ MONAD ALERTimport unfiltered.request.QParams._case req @ Params(params) => val expected = for { lat <- lookup("lat") is required("missing") lon <- lookup("lon") is required("missing") } yield { Ok ~> PlainTextContent ~> ResponseString("%s, %s".format(lat.get, lon.get)) } expected(params) orFail { failures => BadRequest ~> ResponseString(failures.mkString)
    25. 25. But wait, theres more- OAuth- Secure switch - Upload- Routing kit - Netty binding- Basic authentication - Web Sockets
    26. 26. Key takeawaysSimple ☺Retain control ☺Used for real apps ☺Hit the ground running ☺Encode your own policies ☺The source code is the documentation ⚇Typesafe & extensible ☺☺☺Vibe ☹
    27. 27. Questions?fl Wilfred Springer wilfred@flotsam.nl http://nxt.flotsam.nl/

    ×