Developing Web Apps with Haskell and Yesod: Safety-Driven Web Development

Developing Web Apps with Haskell and Yesod: Safety-Driven Web Development

Michael Snoyman

Language: English

Pages: 396

ISBN: 1491915595

Format: PDF / Kindle (mobi) / ePub


This fast-moving guide introduces web application development with Haskell and Yesod, a potent language/framework combination that supports high-performing applications that are modular, type-safe, and concise. Fully updated for Yesod 1.4, this second edition shows you how Yesod handles widgets, forms, persistence, and RESTful content. Author Michael Snoyman also introduces various Haskell tools to supplement your basic knowledge of the language.

By the time you finish this book, you’ll create a production-quality web application with Yesod’s ready-to-use scaffolding. You’ll also examine several real-world examples, including a blog, a wiki, a JSON web service, and a Sphinx search server.

  • Build a simple application to learn Yesod’s foundation datatype and Web Application Interface (WAI)
  • Output HTML, CSS, and Javascript with Shakespearean template languages
  • Get an indepth look at Yesod’s core monads for producing cleaner, more modular code
  • Probe Yesod’s internal workings: learn the request handling process for a typical application
  • Build forms on top of widgets by implementing the yesod-form declarative API
  • Learn how Yesod and Haskell handle persistence and session data
  • Serve an HTML page and a machine-friendly JSON page from the same URL

PHP for Absolute Beginners (2nd Edition)

JavaScript Programming for the Absolute Beginner

Drupal 7

HTML, XHTML, and CSS: Introductory (6th Edition)

Content Everywhere

 

 

 

 

 

 

 

 

 

 

 

 

HandlersMonadResource typeclass, Performance and Error Messages, Writing Handlersmonads, Yesod’s Monads-Summaryadding a new monad transformer, Adding a New Monad Transformer-Adding a New Monad Transformerdatabase-driven navbar (example), Example: Database-Driven NavbarHandlerT App IO monad stack, The HandlerT Monad Transformermonad transformers, Monad Transformersperformance and error messages, Performance and Error Messagesrequest information (example), Example: Request InformationMonadTrans

= forall a. Show a => HaskellShow a instance ToContent HaskellShow where toContent (HaskellShow x) = toContent $ show x instance ToTypedContent HaskellShow where toTypedContent = TypedContent mimeType . toContent getHomeR :: Handler HaskellShow getHomeR = return $ HaskellShow person where person = Person "Michael" 28 main :: IO () main = warp 3000 App The magic here lies in two typeclasses. As we mentioned before, ToContent tells how to convert a value into a raw response. In our case, we would

$forall person <- people'
#{personName person}
#{show $ personAge person} |] main :: IO () main = warp 3000 App Notice that in this case, we didn’t even have to call handlerToWidget. The reason is that a number of the functions included in Yesod automatically work for both Handler and Widget, by means of the MonadHandler typeclass. In fact, MonadHandler will allow these functions to be “autolifted” through many common monad transformers. But if you want to, you can wrap up the call

creates an instance of the RenderMessage typeclass, which is the core of Yesod’s i18n. It is defined as: class RenderMessage master message where renderMessage :: master -> [Text] -- ^ languages -> message -> Text Notice that there are two parameters to the RenderMessage class: the master site and the message type. In theory, we could skip the master type here, but that would mean that every site would need to have the same set of translations for each message type. When it comes to shared

escapeChar . unTextarea where escapeChar '<' = "<" escapeChar '>' = ">" escapeChar '&' = "&" escapeChar c = T.singleton c buildExcerpts takes four parameters: the configuration options, the textual contents of the document, the search index, and the search term. The interesting bit is that we entity-escape the text content. Sphinx won’t automatically escape these for us, so we must do it explicitly. Similarly, the result from Sphinx is a list of Texts. But of course, we’d rather have

Download sample

Download