lite.hs 660 B

12345678910111213141516171819202122
  1. {-# LANGUAGE OverloadedStrings #-}
  2. import Yesod.Core
  3. import Data.Aeson
  4. import Data.Monoid ((<>))
  5. import Data.Text (Text, pack)
  6. people :: [(Text, Int)]
  7. people = [("Alice", 25), ("Bob", 43), ("Charlie", 37)]
  8. main = warp 3000 $ liteApp $ do
  9. onStatic "people" $ dispatchTo getPeople
  10. onStatic "person" $ withDynamic $ dispatchTo . getPerson
  11. getPeople = return $ toJSON $ map fst people
  12. getPerson name =
  13. case lookup name people of
  14. Nothing -> notFound
  15. Just age -> selectRep $ do
  16. provideRep $ return $ object ["name" .= name, "age" .= age]
  17. provideRep $ return $ name <> " is " <> pack (show age) <> " years old"