Original author(s) | Michael Snoyman |
---|---|
Developer(s) | Michael Snoyman et al. |
Initial release | 2010 |
Written in | Haskell |
Operating system | Cross-platform |
Available in | English |
Type | Web framework |
License | MIT |
Website | {{{1}}} |
Yesod (IPA: [je'sod]; Hebrew: יְסוֺד, "Foundation") is a web framework based on the programming language Haskell for productive development of type-safe, representational state transfer (REST) model based (where uniform resource locators (URLs) identify resources, and Hypertext Transfer Protocol (HTTP) methods identify transitions), high performance web applications, developed by Michael Snoyman, et al. It is free and open-source software released under an MIT License.
Yesod is based on templates, to generate instances for listed entities, and dynamic content process functions, through Template Haskell constructs to host domain-specific language (eDSL) content templates called QuasiQuotes, where the content is translated into code expressions by metaprogramming instructions.[1]
There are also web-like language snippet templates that admit code expression interpolations, making them fully type-checked at compile time.[2]
Yesod divides its functions in separate libraries (database, html rendering, forms, etc.) so functions may used as needed.
Yesod uses a Web application interface (WAI),[3] a type of application programming interface (API), to isolate servlets, aka web apps., from servers, with handlers for the server protocols Common Gateway Interface (CGI),[4] FastCGI,[5] Simple Common Gateway Interface (SCGI),[6] Warp,[7] Launch (open as local URL to the default browser, closing the server when the window is closed),[8]
See ref.[9] Yesod requires a data type that instantiates the model–view–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 (like "HomeR") 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 (e.g. web resource names and handler names).
By inserting a mkYesod call, this will call Template Haskell primitives to generate the code[10] 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!" program example based on a Common Gateway Interface (CGI) server interface (the handler types have changed, but the philosophy remains):
{- file wai-cgi-hello.hs -} {-# LANGUAGE PackageImports, TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings #-} import "wai" Network.Wai import "wai-extra" Network.Wai.Handler.CGI (run) -- interchangeable WAI handler import "yesod" Yesod import "yesod-core" Yesod.Handler (getRequest) import "text" Data.Text (Text) import "shakespeare" Text.Cassius (Color(..), colorBlack) -- 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" [parseRoutes| / HomeR GET |] instance Yesod MyApp -- indentation structured CSS template myStyle :: [Text] → CssUrl url myStyle paramStyle = [cassius| .box border: 1px solid #{boxColor} |] where boxColor = case paramStyle of ["high-contrast"] → colorBlack _ → Color 0 0 255 -- indentation structured HTML template myHtml :: [(Text, Text)] → HtmlUrl url myHtml params = [hamlet|
<!-- indentation, or lack of it, under starting tags or commands ('$' prefix) describe the content or sequence tree structure --> <!-- '.' or '#' prefixes in tags introduce css styled "class" or "id" attribute values --> <!-- interpolation of haskell expressions follow the "shakespeare templates" #{expr} syntax --> <p>Hello World! There are <span .box>#{length params} parameters</span>: $if null params <p>Nothing to list $else <ul> $forall param <- params <li>#{fst param}: #{snd param} |]
getHomeR :: Handler RepHtml getHomeR = do req <- getRequest let params = reqGetParams req paramStyle <- lookupGetParams "style" defaultLayout $ do -- adding widgets to the Widget monad (a ''Writer'' monad) setTitle "Yesod example" toWidgetHead $ myStyle paramStyle toWidgetBody $ myHtml params -- there are ''run'' function variants for different WAI handlers main = toWaiApp MyApp >>= run
# cgi test export REMOTE_ADDR=127.0.0.1 export REQUEST_METHOD=GET export PATH_INFO=/ export QUERY_STRING='p1=abc;p2=def;style=high-contrast' ./wai-cgi-hello
See ref.[11][12] Yesod follows the representational state transfer model of access to web documents, identifying docs. and directories as resources with a Route constructor, named with an uppercase R suffix (for example, HomeR).
URL segment capture as parameter is possible specifying a '#' prefix for single segment capture or '*' for multisegment capture, followed by the parameter type.
-- given a MyApp foundation type mkYesod "MyApp" [parseRoutes| / HomeR -- no http methods stated: all methods accepted /blog BlogR GET POST -- the '#' prefix specify the path segment as a route handler parameter /article/#ArticleId ArticleR GET PUT -- the '*' prefix specify the parameter as a sequence of path pieces /branch/*Texts BranchR GET -- to simplify the grammar, compound types must use an alias, eg. type Texts for ''[Text]'' |]
data Route MyApp = HomeR -- referenced in templates as: @{HomeR} | BlogR -- in templates: @{BlogR} | ArticleR ArticleId -- in templates: @{ArticleR myArticleId} | BranchR Texts -- in templates: @{BranchR myBranchSegments}
-- for "/ HomeR" -- no http methods stated ⇒ only one handler with prefix ''handler'' handlerHomeR :: HasReps t ⇒ Handler t -- for "/blog BlogR GET POST" getBlogR :: HasReps t ⇒ Handler t postBlogR :: HasReps t ⇒ Handler t -- for "/article/#ArticleId ArticleR GET PUT" getArticleR :: HasReps t ⇒ ArticleId → Handler t putArticleR :: HasReps t ⇒ ArticleId → Handler t
See ref.[11]
See ref.[13] Authentication plugins: OpenID, BrowserID, Email, GoogleEmail, HashDB, RpxNow.[14]
See ref.[16] Session back-ends: ClientSession[17] (it stores the session in a cookie), ServerSession[18][19] (it stores most of the session data at the server)
A success, failure or indicative message can be stored (setMessage) in the Session and will be shown, if it exists, by the default_layout routine through the default_layout.hamlet
template, being cleared on consultation.[20]
Common URL prefix subsites for workflows, file serving or site partitioning. See ref.[21][22]
Built-in subsites: Static,[23][24] Auth[25]
See ref.[26]
The Form type here is an object that is used in the controller to parse and process the form fields user input and produce a (FormResult, Widget) pair were the widget holds the layout of the next rendering of the form with error messages and marks. It can also be used to generate a new form with blanks or default values.
The form type takes the shape of a function of an html snippet to be embedded in the view, that will hold security purpose hidden fields.
A form object is generated from an Applicative – Monadic composition of fields for a combined, sequential parsing of field inputs.
There are three types of forms:
The field generators, whose names are composed by the form type initial (a|m|i)
followed by (req|opt){- required or optional -}
, have a fieldParse component and a fieldView one.[27]
runForm{Post|Get}
runs the field parsers against the form field inputs and generates a (FormResult, Widget) pair from the views offering a new form widget with the received form field values as defaults. The function suffix is the http method used in the form submission.generateForm{Post|Get}
ignores inputs from the client and generates a blank or defaults form widget.[28]The actual function parameters and types have changed through Yesod versions. Check the Yesod book and libraries signatures.
The magic is in the FormResult data type Applicative instance, where (<*>) collects the error messages for the case of FormFailure [textErrMsg]
result values[29]
Monadic forms permit free form layout and better treatment of hiddenField members.[26]
A sample of an Applicative[30] form:
-- a record for our form fields data Person = Person {personName :: Text, personAge :: Int, personLikings :: Maybe Text} -- the Form type has an extra parameter for an html snippet to be embedded, containing a CSRF token hidden field for security type Form sub master x = Html → MForm sub master (FormResult x, Widget) {- -- for messages in validation functions: @param master: yesod instance to use in renderMessage (return from handler's getYesod) @param languages: page languages to use in renderMessage -- optional defaults record: @param mbPersonDefaults: Just defaults_record, or Nothing for blank form -} personForm :: MyFoundationType → [Text] → Maybe Person → Form sub master Person {- ''aopt'' (optional field AForm component) for "Maybe" fields, ''areq'' (required fld AForm comp.) will insert the "required" attribute -} personForm master languages mbPersonDefaults = renderTable $ Person <$> areq textField fldSettingsName mbNameDefault <*> areq customPersonAgeField fldSettingsAge mbAgeDefault <*> aopt textareaField fldSettingsLikings mbLikingsDefault where mbNameDefault = fmap personName mbPersonDefaults mbAgeDefault = fmap personAge mbPersonDefaults mbLikingsDefault = fmap personLikings mbPersonDefaults -- "fieldSettingsLabel" returns an initial fieldSettings record -- recently the "FieldSettings" record can be defined from a String label since it implements IsString fldSettingsName = (fieldSettingsLabel MsgName) {fsAttrs = [("maxlength","20")]} fldSettingsAge = fieldSettingsLabel MsgAge fldSettingsLikings = (fieldSettingsLabel MsgLikings) {fsAttrs = [("cols","40"),("rows","10")]} customPersonAgeField = check validateAge intField validateAge y | y < 18 = Left $ renderMessage master languages MsgUnderAge | otherwise = Right y
The types shown correspond to an older version, but the philosophy remains.
The Handler monad returns content in one or more of several formats as components of types that implement the HasReps class[31] {RepHtml, RepJson, RepXml, RepPlain, the dual RepHtmlJson, a pair or list of pairs [(ContentType, Content)]
, ..}.[32][33] Json examples:[34][35][36]
The HasReps default implementation of chooseRep chooses the document representation to be returned according to the preferred content-type list of the client accept header.[31]
A Widget monad,[38] based on a Writer[39] one and argument to defaultLayout, facilitate to piece the widgets together.
[qq| ... |]
introduces an indentation based structured html template. (See doc.[41]).[42]'$' prefixes lines of logic statements.
Automatic closing tags are generated only for the tag at line start position.
toWidget [hamlet| $doctype 5 <html> <!-- only the tag at the beginning of the line will be automatically closed --> <!-- '.' or '#' prefixes in tags introduce class/id names, à la CSS --> <!-- ":boolVar:" prefix in attributes makes them conditionally generated --> <!-- interpolation of haskell expressions follow the "shakespearean templates" syntax introduced in the so named section --> <head> <title>#{pageTitle} - My Site <link rel=stylesheet href=@{Stylesheet_route}> <body> <header> ^{headerTemplate} <section #mySectionId> <p><span .titleClass>_{MsgArticleListTitle}</span> $if null articles <p :isRed:style="color:red">_{MsgSorryNoArticles} $else <ul> $forall art <- articles <li>#{articleNumber art} .- #{articleTitle art} <footer> ^{footerTemplate} |]
See ref.[41] These are content view templates that follow a common substitution pattern of code expressions within curly brackets with different character prefix to refer to
^{...}
^{template params}
,@{...}
@{HomeR}
,_{...}
_{MsgMessageLabel params}
#{...}
#{haskell_expression}
which type must be convertible<isoLanguage>
.msg" files) with parameter interpolations, the expression type must be an instance of Text.Shakespeare.I18N.ToMessage [46]Using non-English text in expressions requires use of the Unicode-aware type Text, since the Glasgow Haskell Compiler's (GHC's) show for the type String renders non-ASCII characters as escaped numerical codes.
See ref.[56]
Yesod app messages are localizable (i18n). They should be held within the messages folder, in files named based on ISO, as <iso-language>.msg
Message entries follow the EBNF pattern:
-- EBNF: identifier, {' ', parameter, '@', type}, ":", text with interpolations ArticleUnexistent param@Int64 : unexistent article #{param}
-- in code myMsg :: MyAppMessage -- datatype appending "Message" to the foundation type myMsg = MsgArticleUnexistent myArticleId -- constructor prepending "Msg" to the msg. label -- in widget templates _{MsgArticleUnexistent myArticleId}
Actual i18n support is missing from the stack app template. The mkMessage "MyApp" messagesFolder isoLangDefault
must be added to the "Foundation.hs" file to get the messages instantiated.[57]
E.g. a visitor count. See ref.[61]
There is first class support for PostgreSQL, SQLite, MongoDB, CouchDB and MySQL, with experimental support for Redis.[62]
The Database layout is described in a template listing the entities, fields and constraints.[65]
share [mkPersist sqlSettings, mkMigrate "migrateAll" -- generates the migration procedure with the specified name ] [persist| User -- table name and entity record type -- implicit autoincrement column "id" as primary key, typed UserId ident Text -- refers to db. table column "ident"; -- generates a record field prefixing the table name as "userIdent" password Text Maybe -- Maybe indicates Nullable field UniqueUser ident -- unique constraint with space sep. field sequence Email -- table name and entity record type -- implicit autoincrement column "id" as primary key, typed EmailId email Text user UserId -- foreign key by specifying other tables EntityField types verkey Text Maybe newlyAddedColumn Text "default='sometext'::character varying" -- sql level Default constraint UniqueEmail email -- unique constraint |]
Example for persistent rawSQL and Esqueleto queries.[70]
The following packages are part of the yesod-platform:[71]
New Yesod apps are generated from the HaskellStack tool[75] templates, replacing previous command "yesod init"
Stack based app. template names are prefixed by yesod as "yesod-{minimal | postgres | sqlite | mysql | mongo | ...}"
yesod devel
run from the project site, recompiles and restarts the project at every file tree modification.yesod add-handler
adds a new handler and module to the project, adding an import clause for the handler in the "Application" module.Keter is a process as a service that handles deployment and restart of Yesod web app servers, and, per web app, database creation for PostgreSQL.
The console command yesod keter
packs the web app. as a keter bundle for uploading to a keter folder named "incoming".
Keter monitors the "incoming" folder and unpacks the app. to a temporary one, then assigns the web app a port to listen to, and starts it.
Initially it worked with Nginx as reverse proxy (keter version 0.1*), adding virtual server entries to its configuration and making Nginx reload it, but now Keter itself provides its own reverse proxy functionality, removing Nginx dependency and acting as the main web server.[80]
Old documentation (Nginx based).[81][82]
Original source: https://en.wikipedia.org/wiki/Yesod (web framework).
Read more |