Yesod (web framework)


Yesod is a free and open-source web framework based on Haskell for productive development of type-safe, REST model based, high performance web applications, developed by Michael Snoyman et al.
Yesod is based on templates, to generate instances for listed entities, and dynamic content process functions, through Template Haskell constructs to host eDSL content templates called QuasiQuotes, where the content is translated into code expressions by metaprogramming instructions.
There are also web-like language snippet templates that admit code expression interpolations, making them fully type-checked at compile-time.
Yesod divides its functionality in separate libraries, so you may choose your functionality library of your choice.

MVC architecture

Controller

Server interface

Yesod uses a Web application interface API, abbreviated WAI, to isolate servlets, aka web apps., from servers, with handlers for the server protocols CGI, FastCGI, SCGI, Warp, Launch,

The ''foundation'' type

See ref. Yesod requires a data type that instantiates the controller classes. This is called the foundation type. In the example below, it is named "MyApp".
The REST model identifies a web resource with a web path. Here REST resources are given names with an R suffix and are listed in a parseRoutes site map description template. From this list, route names and dispatch handler names are derived.
Yesod makes use of Template Haskell metaprogramming to generate code from templates at compile time, assuring that the names in the templates match and everything typechecks.
By inserting a mkYesod call, this will call Template Haskell primitives to generate the code corresponding to the route type members, and the instances of the dispatch controller classes as to dispatch GET calls to route HomeR to a routine named composing them both as "getHomeR", expecting an existing handler that matches the name.

Hello World

"Hello world" example based on a CGI server interface :

import "wai" Network.Wai
import "wai-extra" Network.Wai.Handler.CGI -- interchangeable WAI handler
import "yesod" Yesod
import "yesod-core" Yesod.Handler
import "text" Data.Text
import "shakespeare" Text.Cassius
-- the Foundation type
data MyApp = MyApp
-- sitemap template, listing path, resource name and methods accepted
-- `mkYesod` takes the foundation type name as param. for name composition of dispatch functions
mkYesod "MyApp" → CssUrl url
myStyle paramStyle =
→ colorBlack
_ → Color 0 0 255
-- indentation structured HTML template
myHtml :: → HtmlUrl url
myHtml params =


Hello World! There are # parameters:
$if null params

Nothing to list
$else