The Wheb package

[Tags: bsd3, library]

Wheb's a framework for building robust, high-concurrency web applications simply and effectively.

Plugins:

Wheb makes it easy to write plugins. Plugins can add routes, middlware, settings and even handle resource cleanup on server shutdown. Named routes allow plugins to dynamically generate their routes at runtime based on settings.

Examples of plugins:

Wheb in action:

Use with language extensions OverloadedStrings

 import           Web.Wheb

 main :: IO ()
 main = do
   opts <- genMinOpts $ do
      addGET "home" rootPat $ text "Hi!"
      addGET "about" ("about" </> "something") $ html "<html><body><h1>About!</h1></body></html>"
   runWhebServer opts

Bigger example (Stateful.hs):

Wheb makes it easy to share a global context and handle requests statefully. The Wheb monad is both a Reader and a State Monad allowing you to seperate thread-safe resources.

Below is an example of site that naively counts the non-unique hits across all pages. MyApp is our Reader's type and MyHandlerData is our State's type. MyApp is shared across requests while MyHandlerData is thread specific with a starting state given in options. We have a middleware that intercepts the request, safely increments the shared resource TVar and sets our MyHandlerData to the correct count before it reaches our handler. We use a TVar in the Global context because any state changes to the handler state will not affect other requests.

  import           Control.Concurrent.STM
  import           Control.Monad.IO.Class
  import           Data.Monoid
  import           Data.Text.Lazy (Text)
  import           Web.Wheb

  data MyApp = MyApp Text (TVar Int)
  data MyHandlerData = MyHandlerData Int

  counterMw :: MonadIO m => WhebMiddleware MyApp MyHandlerData m
  counterMw = do
    (MyApp _ ctr) <- getApp
    number <- liftIO $ atomically $ do
            num <- readTVar ctr
            writeTVar ctr (succ num)
            return num
    putHandlerState (MyHandlerData number)
    return Nothing

  homePage :: WhebHandler MyApp MyHandlerData
  homePage = do
    (MyApp appName _)   <- getApp
    (MyHandlerData num) <- getHandlerState
    html $ ("<h1>Welcome to" <> appName <>
            "</h1><h2>You are visitor #" <> (spack num) <> "</h2>")

  main :: IO ()
  main = do
    opts <- generateOptions $ do
              startingCounter <- liftIO $ newTVarIO 0
              addWhebMiddleware counterMw
              addGET "." rootPat $ homePage
              return $ (MyApp "AwesomeApp" startingCounter, MyHandlerData 0)
    runWhebServer opts

Properties

Versions0.0.1.0, 0.0.1.1, 0.1.0.0, 0.2.0.0, 0.3.0.0, 0.3.1.0
Dependenciesbase (==4.7.*), blaze-builder (==0.3.*), bytestring (==0.10.*), case-insensitive (==1.2.*), containers (==0.5.*), cookie (==0.4.*), http-types (==0.8.*), mtl (>=2.1 && <2.3), pwstore-fast (==2.4.*), stm (==2.4.*), text (>=1.0 && <1.2), time (==1.4.*), transformers (==0.4.*), unix (==2.7.*), uuid (==1.3.*), wai (==3.0.*), wai-extra (==3.0.*), wai-websockets (==3.0.*), warp (==3.0.*), web-routes (==0.27.*), websockets (==0.8.*)
LicenseBSD3
AuthorKyle Hanson
Maintainerme@khanson.io
CategoryWeb
Home pagehttps://github.com/hansonkd/Wheb-Framework
Source repositoryhead: git clone git://github.com/hansonkd/Wheb-Framework.git
UploadedSun Sep 14 03:29:03 UTC 2014 by hansonkd
Downloads520 total (73 in last 30 days)
StatusDocs uploaded by user
Build status unknown [no reports yet]

Modules

[Index]

Downloads

Maintainers' corner

For package maintainers and hackage trustees