Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Introduction MVC layer Deployment Outline 1 Introduction 2 MVC layer 3 Deployment Sibi Prabakaran Web programming in Haskell using Yesod

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Introduction MVC layer Deployment Persistent Database agnostic Data modeling Migrations Sibi Prabakaran Web programming in Haskell using Yesod

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Introduction MVC layer Deployment Hamlet $doctype 5 < t i t l e >#{ pageTitle } − My Site

#{ pageTitle }

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

Sorry , I l i e d

You don ’ t have any f r i e n d s . $else

    $ 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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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 (' in je ct ed ')& l t ; / s c r i p t&gt ; Sibi Prabakaran Web programming in Haskell using Yesod

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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 (, tags) Sibi Prabakaran Web programming in Haskell using Yesod

Slide 20

Slide 20 text

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 | | ] toWidget [ hamlet |

Here ’ s one way of including content | ] [ whamlet|

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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