{"id":475,"date":"2012-07-21T16:43:16","date_gmt":"2012-07-21T23:43:16","guid":{"rendered":"http:\/\/www.lothlorien.com\/kf6gpe\/?p=475"},"modified":"2012-07-21T16:43:16","modified_gmt":"2012-07-21T23:43:16","slug":"hello-world-yesod-style","status":"publish","type":"post","link":"https:\/\/www.lothlorien.com\/kf6gpe\/hello-world-yesod-style\/","title":{"rendered":"&#8220;Hello World&#8221;, Yesod style&#8230;"},"content":{"rendered":"<p>Here we go! Without further ado, borrowed in part from <em>Developing Web Applications with Haskell and Yesod<\/em><br \/>\n(although the discussion that follows is mine, taken from what I now understand about the code).<\/p>\n<p><code><br \/>\n{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses,<br \/>\n             TemplateHaskell, OverloadedStrings #-}<\/p>\n<p>import Yesod<\/p>\n<p>data HelloWorld = HelloWorld<\/p>\n<p>mkYesod \"HelloWorld\" [parseRoutes|<br \/>\n\/ HomeR GET<br \/>\n|]<\/p>\n<p>instance Yesod HelloWorld<\/p>\n<p>getHomeR :: Handler RepHtml<br \/>\ngetHomeR = defaultLayout [whamlet|Hello World!|]<\/p>\n<p>main :: IO ()<br \/>\nmain = warpDebug 3000 HelloWorld<br \/>\n<\/code><\/p>\n<p>This makes use of several extensions not part of the Haskell standard:<\/p>\n<ul>\n<li>Type families<\/li>\n<li>Quasiquoting, used for lots of little domain specific languages (DSLs) like the one used to specify the binding between the front controller and handler methods<\/li>\n<li>Templated Haskell &#8212; a lot of Yesod code generates other code that actually supports your Web application<\/li>\n<\/ul>\n<p>We&#8217;re going to create one data type that&#8217;s the <em>foundation<\/em> of our Web application. In a bigger Web app, this data type would have all the stuff needed for initialization, too &#8212; stuff like bringing up a persistent database connection. Our foundation is <code>HelloWorld<\/code>.<\/p>\n<p>The next bit of code uses the <code>parseRoutes<\/code> DSL to set up the binding between the Web application&#8217;s URIs and the functions that the framework invokes when requests for those URIs come in &#8212; the &#8220;resource&#8221;, as denoted by the trailing R in the function&#8217;s name. The syntax is <em>path to resource<\/em>, then whitespace, then the <em>resource<\/em>, then more whitespace, then the HTTP method used to access the resource. <\/p>\n<p>We indicate that <code>HelloWorld<\/code> is an instance of <code>Yesod<\/code>, the type class encapsulating the behavior of Yesod&#8217;s framework. <\/p>\n<p>Next, we declare the resource handling function for <code>\/<\/code>. The framework expects the resource handler to be named as a concatenation of the HTTP method used to access the resource and the resource itself &#8212; hence <code>getHomeR<\/code> is the resource for handling <code>GET<\/code> requests of the <code>HomeR<\/code> resource, which we bound to the path <code>\/<\/code>. It&#8217;s of type <code>Handler RepHTML<\/code>, indicating that it returns HTML. Ours uses the <code>whamlet<\/code> quasiquoter to take an HTML-based DSL and return actual well-formed HTML to the client. As I&#8217;ll get into in a later post, you can evaluate Haskell and do other nifty stuff in the context of the various DSLs for content here &#8212; it&#8217;s pretty cool. Yesod refers to these bits of HTML as <em>widgets<\/em>, and they can be put in different files so you can have a web page built up of small, reusable widget components, each written in a DSL for HTML called <em>Hamlet<\/em>. <\/p>\n<p>Our handler just returns <code>Hello World!<\/code>.<\/p>\n<p>Next up, we have our main function, invoked when the application starts. <code>main<\/code> uses a built-in web server, Warp, running in this case on port 3000, wired to the HelloWorld class. <\/p>\n<p>We can run all of this using <code>runhaskell helloworld.hs<\/code> on our local development box, and we&#8217;re rewarded with a running Web application on port 3000 &#8212; to hit it, start the application in a terminal window, and then go to <a href=\"http:\/\/localhost:3000\/\" target=\"_blank\">http:\/\/localhost:3000<\/a> in a browser window. You can see what HTML the framework produced by choosing &#8220;View source&#8221;, too.<\/p>\n<p>Apparently, you don&#8217;t have to deploy with Warp, although some people do. You can also tether this to a CGI, or even deploy it in a stand-alone WebKit application! This last idea really interests me &#8212; you could build a pretty complicated application using Yesod and WebKit, and package it up as a stand-alone binary, I guess. You&#8217;d be limited, of course, to what WebKit could present, but with what JavaScript and HTML5 can do together, that&#8217;s an interesting way to slap a nice UI on a larger Haskell project, especially if it&#8217;s backed by a SQLite database for data storage. I&#8217;m going to have to think some more about that.<\/p>\n<p>Next up, expect either some musings on that score, or perhaps a more in-depth look at Hamlet, the HTML templating language! In the mean time, I encourage you to play around with the resource DSL set up by <code>parseRoutes<\/code> &#8212; try adding content to your &#8220;application&#8221;. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here we go! Without further ado, borrowed in part from Developing Web Applications with Haskell and Yesod (although the discussion that follows is mine, taken from what I now understand about the code). {-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings #-} import Yesod data HelloWorld = HelloWorld mkYesod &#8220;HelloWorld&#8221; [parseRoutes| \/ HomeR GET |] instance &hellip; <a href=\"https:\/\/www.lothlorien.com\/kf6gpe\/hello-world-yesod-style\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">&#8220;Hello World&#8221;, Yesod style&#8230;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,44],"tags":[27,29],"class_list":["post-475","post","type-post","status-publish","format-standard","hentry","category-haskell","category-yesod","tag-haskell","tag-web"],"_links":{"self":[{"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/posts\/475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/comments?post=475"}],"version-history":[{"count":9,"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/posts\/475\/revisions"}],"predecessor-version":[{"id":484,"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/posts\/475\/revisions\/484"}],"wp:attachment":[{"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/media?parent=475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/categories?post=475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/tags?post=475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}