Haskell Language Web Development Yesod

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Yesod project can be created with stack new using following templates:

  • yesod-minimal. Simplest Yesod scaffold possible.
  • yesod-mongo. Uses MongoDB as DB engine.
  • yesod-mysql. Uses MySQL as DB engine.
  • yesod-postgres. Uses PostgreSQL as DB engine.
  • yesod-postgres-fay. Uses PostgreSQL as DB engine. Uses Fay language for front-end.
  • yesod-simple. Recommended template to use, if you don't need database.
  • yesod-sqlite. Uses SQlite as DB engine.

yesod-bin package provides yesod executable, which can be used to run development server. Note that you also can run your application directly, so yesod tool is optional.

Application.hs contains code that dispatches requests between handlers. It also sets up database and logging settings, if you used them.

Foundation.hs defines App type, that can be seen as an environment for all handlers. Being in HandlerT monad, you can get this value using getYesod function.

Import.hs is a module that just re-exports commonly used stuff.

Model.hs contains Template Haskell that generates code and data types used for DB interaction. Present only if you are using DB.

config/models is where you define your DB schema. Used by Model.hs.

config/routes defines URI's of the Web application. For each HTTP method of the route, you'd need to create a handler named {method}{RouteR}.

static/ directory contains site's static resources. These get compiled into binary by Settings/StaticFiles.hs module.

templates/ directory contains Shakespeare templates that are used when serving requests.

Finally, Handler/ directory contains modules that define handlers for routes.

Each handler is a HandlerT monad action based on IO. You can inspect request parameters, its body and other information, make queries to the DB with runDB, perform arbitrary IO and return various types of content to the user. To serve HTML, defaultLayout function is used that allows neat composition of shakespearian templates.



Got any Haskell Language Question?