Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web programming in Haskell using Yesod

Sibi
December 17, 2017

Web programming in Haskell using Yesod

Sibi

December 17, 2017
Tweet

More Decks by Sibi

Other Decks in Technology

Transcript

  1. Introduction MVC layer Deployment Web Programming in Haskell using Yesod

    Bangalore Haskell User Group Meetup Xebia India Dec 16, 2017 Sibi Prabakaran Web programming in Haskell using Yesod
  2. Introduction MVC layer Deployment About Me Sibi Consultant for Xebia

    Internet Handle: psibi Mail: [email protected] http://psibi.in Sibi Prabakaran Web programming in Haskell using Yesod
  3. Introduction MVC layer Deployment Outline 1 Introduction 2 MVC layer

    3 Deployment Sibi Prabakaran Web programming in Haskell using Yesod
  4. Introduction MVC layer Deployment Goal Viable choice Yesod’s philosophy Survey

    of yesod related libraries, ORM backends etc. Real world experience Sibi Prabakaran Web programming in Haskell using Yesod
  5. Introduction MVC layer Deployment History Yesod is a Hebrew word

    Originally developed by Michael Snoyman Initial release: March 8, 2010 License: MIT Sibi Prabakaran Web programming in Haskell using Yesod
  6. Introduction MVC layer Deployment Why Yesod? R evolutionary framework Avoid

    bugs at compile time DSLs for common tasks Performant Sibi Prabakaran Web programming in Haskell using Yesod
  7. Introduction MVC layer Deployment Declare tables mkPersist [ p e

    r s i s t | User name String age Int Todo userId UserId task Text done Bool | ] Other syntax: sqltype, constraint, sql etc. Deriving json Reference: Persisent docs Sibi Prabakaran Web programming in Haskell using Yesod
  8. Introduction MVC layer Deployment CRUD simonId <− insert $ "SPJ"

    34 marlowId <− insert $ " Marlow " 35 insert_ $ simonId "Buy dragon book " False simonTasks <− s e l e c t L i s t [ TodoUserId ==. simonId ] [ LimitTo 5] ( simon : : Maybe User ) <− get simonId delete marlowId deleteWhere [ TodoUserId ==. simonId ] Sibi Prabakaran Web programming in Haskell using Yesod
  9. Introduction MVC layer Deployment Routing Single data type for representing

    URLs Mapping between route to handler is done Two way parsing functions created Sync between parsing and handler functions ensured Sibi Prabakaran Web programming in Haskell using Yesod
  10. Introduction MVC layer Deployment Route syntax / HomeR GET POST

    / hello HelloR / f i b /# Int FibR GET Canonical URLs (joinPath, cleanPath) Pieces: Static, Dynamic single, Dynamic multi Sibi Prabakaran Web programming in Haskell using Yesod
  11. Introduction MVC layer Deployment Routing internals data MyAppRoute = HomeR

    | HelloR | FibR Int renderMyAppRoute HomeR = [ ] renderMyAppRoute HelloR = [ " hello " ] renderMyAppRoute ( FibR i n t ) = [ " f i b " , toSinglePiece i n t ] parseMyAppRoute [ ] = Just HomeR parseMyAppRoute [ " hello " ] = Just HelloR parseMyAppRoute [ " f i b " , i n t ] = do f i b I n t <− fromSinglePiece i n t return $ FibR f i b I n t parseMyAppRoute _ = Nothing Sibi Prabakaran Web programming in Haskell using Yesod
  12. Introduction MVC layer Deployment Template languages DSLs for templating Compile

    time syntax checked Variable interpolation Control structures for Hamlet Sibi Prabakaran Web programming in Haskell using Yesod
  13. Introduction MVC layer Deployment Hamlet $doctype 5 <html> <head> <

    t i t l e >#{ pageTitle } − My Site <link rel =stylesheet href=@{ StylesheetR }> <body> <h1 . page−t i t l e >#{ pageTitle } <p>Here i s a l i s t of your f r i e n d s : $ i f n u l l fri e nd s <p>Sorry , I l i e d <p>You don ’ t have any f r i e n d s . $else <ul> $ f o r a l l Friend name age <− f r i e nd s < l i >#{name} (#{ age } years old ) < footer >^{ copyright } Sibi Prabakaran Web programming in Haskell using Yesod
  14. Introduction MVC layer Deployment Lucius & Cassius (CSS) Lucius section

    . blog { padding : 1em; border : 1px s o l i d #000; h1 { color : #{ headingColor } ; background−image : u r l (@{ MyBackgroundR } ) ; } } Cassius section . blog padding : 1em border : 1px s o l i d #000 h1 color : #{ headingColor } background−image : u r l (@{ MyBackgroundR } ) Sibi Prabakaran Web programming in Haskell using Yesod
  15. Introduction MVC layer Deployment Julius (Javascript) $ ( function (

    ) { $ ( " section . # { sectionClass } " ) . hide ( ) ; $ ( "#mybutton " ) . c l i c k ( function ( ) { document . l o c a t i o n = "@{SomeRouteR} " ; } ) ; ^{ addBling } } ) ; Sibi Prabakaran Web programming in Haskell using Yesod
  16. Introduction MVC layer Deployment XSS Protection name : : Text

    name = " Sibi < s c r i p t > a l e r t ( ’ injected ’ ) < / s c r i p t >" main : : IO ( ) main = putStrLn $ renderHtml [ shamlet | # {name } | ] Output: Sibi & l t ; s c r i p t&gt ; a l e r t (&#39; in je ct ed &#39;)& l t ; / s c r i p t&gt ; Sibi Prabakaran Web programming in Haskell using Yesod
  17. Introduction MVC layer Deployment XSS Protection blaze-html package ToMarkup typeclass

    Textual values are always escaped (see ToMarkup instances) Html values aren’t escaped preEscapedToHtml Sibi Prabakaran Web programming in Haskell using Yesod
  18. Introduction MVC layer Deployment Widgets Glue between template languages Ability

    to re-use a single UI component Can perform IO operations (DB queries etc.) Controls the generation of end HTML (<body>,<head> tags) Sibi Prabakaran Web programming in Haskell using Yesod
  19. Introduction MVC layer Deployment Widget: Example getHomeR = defaultLayout $

    do s e t T i t l e "My Page T i t l e " toWidget [ lu cius | h1 { color : green ; } | ] addScriptRemote " https : / / ajax . googleapis .com/ ajax / l i b s / jquery / 1 . 6 . 2 / jquery . min . j s " toWidget [ j u l i u s | $ ( function ( ) { $ ( " h1 " ) . c l i c k ( function ( ) { a l e r t ( "You clicked on the heading ! " ) ; } ) ; } ) ; | ] toWidgetHead [ hamlet | <meta name=keywords content="some sample keywords "> | ] toWidget [ hamlet | <h1>Here ’ s one way of including content | ] [ whamlet| <h2>Here ’ s another | ] toWidgetBody [ j u l i u s | a l e r t ( " This i s included in the body i t s e l f " ) ; | ] Sibi Prabakaran Web programming in Haskell using Yesod
  20. Introduction MVC layer Deployment Deploying Yesod apps Common advice: Don’t

    compile on server Change config file for production (Personally used CPP) Files to deploy: executable, static folder Keter Hapistrano Sibi Prabakaran Web programming in Haskell using Yesod
  21. Introduction MVC layer Deployment Other parts Subsites Middlewares clientsession yesod-form

    yesod-auth yesod-fb yesod-sitemap websocket/eventsource support Lots of others in Hackage Real world experience Sibi Prabakaran Web programming in Haskell using Yesod
  22. Introduction MVC layer Deployment Want to contribute? Github: psibi/yesod-rest yesod-rest

    Github: psibi/wai-slack-middleware wai-slack yesod, persistent etc. Questions... ? Sibi Prabakaran Web programming in Haskell using Yesod