Yesod examples

As of March 2020, School of Haskell has been switched to read-only mode.

Trivial rest API

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}
{-# LANGUAGE TemplateHaskell   #-}
{-# LANGUAGE TypeFamilies      #-}
import           Data.Text (Text)
import           Yesod
import qualified Data.Map as M

data App = App

mkYesod "App" [parseRoutes|
/ HomeR GET
|]

instance Yesod App

getHomeR :: Handler TypedContent
getHomeR = selectRep $ do
    provideRep $ return
        [shamlet|
            <p>Hello, my name is #{name} and I am #{age} years old.
        |]
    provideRep $ do
        value <- requireJsonBody
        let name :: Maybe Text = M.lookup ("name" :: Text) value
        return $ object
          [ "name" .= name
          , "age" .= age
          ]
  where
    name = "Michael" :: Text
    age = 28 :: Int

main :: IO ()
main = warpEnv App