server package:happstack-server

Happstack.Server provides a self-contained HTTP server and a rich collection of types and functions for routing Requests, generating Responses, working with query parameters, form data, and cookies, serving files and more. A very simple, "Hello World!" web app looks like:
import Happstack.Server
main = simpleHTTP nullConf $ ok "Hello World!"
By default the server will listen on port 8000. Run the app and point your browser at: http://localhost:8000/ At the core of the Happstack server we have the simpleHTTP function which starts the HTTP server:
simpleHTTP :: ToMessage a => Conf -> ServerPart a -> IO ()
and we have the user supplied ServerPart (also known as, ServerPartT IO), which generates a Response for each incoming Request. A trivial HTTP app server might just take a user supplied function like:
myApp :: Request -> IO Response
For each incoming Request the server would fork a new thread, run myApp to generate a Response, and then send the Response back to the client. But, that would be a pretty barren wasteland to work in. The model for ServerPart is essential the same, except we use the much richer ServerPart monad instead of the IO monad. For in-depth documentation and runnable examples I highly recommend The Happstack Crash Course http://happstack.com/docs/crashcourse/index.html.
The ServerMonad class provides methods for reading or locally modifying the Request. It is essentially a specialized version of the MonadReader class. Providing the unique names, askRq and localRq makes it easier to use ServerPartT and ReaderT together.
An alias for ServerPartT IO
ServerPartT is a rich, featureful monad for web development. see also: simpleHTTP, ServerMonad, FilterMonad, WebMonad, and HasRqData
ServerPartT is a rich, featureful monad for web development. see also: simpleHTTP, ServerMonad, FilterMonad, WebMonad, and HasRqData
Web related tools and services. Happstack Server provides an HTTP server and a rich set of functions for routing requests, handling query parameters, generating responses, working with cookies, serving files, and more. For in-depth documentation see the Happstack Crash Course http://happstack.com/docs/crashcourse/index.html
Apply a function to transform the inner monad of ServerPartT m. Often used when transforming a monad with ServerPartT, since simpleHTTP requires a ServerPartT IO a. Refer to UnWebT for an explanation of the structure of the monad. Here is an example. Suppose you want to embed an ErrorT into your ServerPartT to enable throwError and catchError in your Monad.
type MyServerPartT e m a = ServerPartT (ErrorT e m) a
Now suppose you want to pass MyServerPartT into a function that demands a ServerPartT IO a (e.g. simpleHTTP). You can provide the function:
unpackErrorT :: (Monad m, Show e) => UnWebT (ErrorT e m) a -> UnWebT m a
unpackErrorT et = do
eitherV <- runErrorT et
return $ case eitherV of
Left err -> Just (Left $ toResponse $
"Catastrophic failure " ++ show err
, filterFun $ \r -> r{rsCode = 500})
Right x -> x
With unpackErrorT you can now call simpleHTTP. Just wrap your ServerPartT list.
simpleHTTP nullConf $ mapServerPartT unpackErrorT (myPart `catchError` myHandler)
Or alternatively:
simpleHTTP' unpackErrorT nullConf (myPart `catchError` myHandler)
Also see spUnwrapErrorT for a more sophisticated version of this function.
A variant of mapServerPartT where the first argument also takes a Request. Useful if you want to runServerPartT on a different ServerPartT inside your monad (see spUnwrapErrorT).
Particularly useful when combined with runWebT to produce a m (Maybe Response) from a Request.
Respond with 500 Internal Server Error.
main = simpleHTTP nullConf $ internalServerError "Sorry, there was an internal server error."