Chapter 20. Yesod for Haskellers

The majority of this book is built around giving practical information on how to get common tasks done, without drilling too much into the details of what’s going on under the surface. This book presumes knowledge of Haskell, but it does not follow the typical style of many introductions to Haskell libraries. Many seasoned Haskellers may be put off by this hiding of implementation details. The purpose of this chapter is to address those concerns. We’ll start off with a bare-minimum web application and build up to more complicated examples, explaining the components and their types along the way.

Hello, Warp

Let’s start off with the most bare-minimum application I can think of:

{-# LANGUAGE OverloadedStrings #-}
import           Network.HTTP.Types       (status200)
import           Network.Wai              (Application, responseLBS)
import           Network.Wai.Handler.Warp (run)

main :: IO ()
main = run 3000 app

app :: Application
app _req sendResponse = sendResponse $ responseLBS
    status200
    [("Content-Type", "text/plain")]
    "Hello, Warp!"

Wait a minute, there’s no Yesod in there! Don’t worry, we’ll get there. Remember, we’re building from the ground up, and in Yesod the ground floor is WAI, the Web Application Interface. WAI sits between a web handler, such as a web server or a test framework, and a web application. In our case, the handler is Warp, a high-performance web server, and our application is the app function.

What’s this mysterious Application type? It’s a type synonym defined as: ...

Get Developing Web Apps with Haskell and Yesod, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.