simple-0.4.0: A minimalist web framework for the WAI server interface

Safe HaskellNone




Controller provides a convenient syntax for writting Application code as a Monadic action with access to an HTTP request, rather than a function that takes the request as an argument. This module also defines some helper functions that leverage this feature. For example, redirectBack reads the underlying request to extract the referer and returns a redirect response:

    myController = do
      if badLogin then



type Controller = ReaderT ControllerState (ResourceT IO)Source

A Controller is a Reader monad that contains the HTTP request in its environment. A Controller is Routeable simply by running the Reader.

Utility functions

redirectBack :: Controller ResponseSource

Redirect back to the referer. If the referer header is not present redirect to root (i.e., /).



:: Response

Fallback Response

-> Controller Response 

Redirect back to the referer. If the referer header is not present fallback on the given Response.



:: Parseable a 
=> ByteString

Parameter name

-> Controller (Maybe a) 

Looks up the parameter name in the request's query string and returns the value as a ByteString or Nothing.

For example, for a request with query string: "?foo=bar&baz=7", queryParam "foo"

would return Just "bar", but

   queryParam "zap"

would return Nothing

parseForm :: Controller ([Param], [(ByteString, FileInfo FilePath)])Source

Parses a HTML form from the request body. It returns a list of Params as well as a list of Files, which are pairs mapping the name of a file form field to a FileInfo pointing to a temporary file with the contents of the upload.

   myController = do
     (prms, files) <- parseForm
     let mPicFile = lookup "profile_pic" files
     case mPicFile of
       Just (picFile) -> do
         sourceFile (fileContent picFile) $$
           sinkFile ("images/" ++ (fileName picFile))
         respond $ redirectTo "/"
       Nothing -> redirectBack

respond :: Routeable r => r -> Controller rSource

An alias for return that's helps the the compiler type a code block as a Controller. For example, when using the Frank routing DSL to define a simple route that justs returns a Response, respond can be used to avoid explicit typing of the argument:

   get "/" $ do
     respond $ okHtml "Hello World"

instead of:

   get "/" $ (do
     return $ okHtml "Hello World") :: Controller Response

Low level functions

request :: Controller RequestSource

Reads the underlying Request

body :: Controller ByteStringSource

Reads and returns the body of the HTTP request.