Snap is the Monad that user web handlers run in. Snap gives you:
Stateful access to fetch or modify an HTTP Request.
printRqContextPath :: Snap () printRqContextPath = writeBS . rqContextPath =<< getRequestStateful access to fetch or modify an HTTP Response.
printRspStatusReason :: Snap () printRspStatusReason = writeBS . rspStatusReason =<< getResponseFailure / Alternative / MonadPlus semantics: a Snap handler can choose not to handle a given request, using empty or its synonym pass, and you can try alternative handlers with the <|> operator:
a :: Snap String a = pass b :: Snap String b = return "foo" c :: Snap String c = a <|> b -- try running a, if it fails then try bConvenience functions (writeBS, writeLBS, writeText, writeLazyText, addToOutput) for queueing output to be written to the Response, or for streaming to the response using io-streams:
example :: (OutputStream Builder -> IO (OutputStream Builder)) -> Snap () example streamProc = do writeBS "I'm a strict bytestring" writeLBS "I'm a lazy bytestring" writeText "I'm strict text" addToOutput streamProcEarly termination: if you call finishWith:
a :: Snap () a = do modifyResponse $ setResponseStatus 500 "Internal Server Error" writeBS "500 error" r <- getResponse finishWith rthen any subsequent processing will be skipped and the supplied Response value will be returned from runSnap as-is.
Access to the IO monad through a MonadIO instance:
a :: Snap () a = liftIO fireTheMissilesThe ability to set or extend a timeout which will kill the handler thread after
Nseconds of inactivity (the default is 20 seconds):a :: Snap () a = setTimeout 30Throw and catch exceptions using a MonadBaseControl instance:
import Control.Exception.Lifted (SomeException, throwIO, catch) foo :: Snap () foo = bar `catch` (e::SomeException) -> baz where bar = throwIO FooExceptionLog a message to the error log:
foo :: Snap () foo = logError "grumble."
You may notice that most of the type signatures in this module contain a
(MonadSnap m) => ... typeclass constraint. MonadSnap is a typeclass
which, in essence, says "you can get back to the Snap monad from
here". Using MonadSnap you can extend the Snap monad with additional
functionality and still have access to most of the Snap functions without
writing lift everywhere. Instances are already
provided for most of the common monad transformers
(ReaderT, Control.Monad.Trans.Writer.WriterT,
StateT, etc.).
Instances11Monad, Functor, MonadFail, Applicative, Alternative, MonadPlus, …
Monad SnapDefined in snap-core-1.0.5.1 · Snap.Internal.CoreFunctor SnapDefined in snap-core-1.0.5.1 · Snap.Internal.CoreMonadFail SnapDefined in snap-core-1.0.5.1 · Snap.Internal.CoreApplicative SnapDefined in snap-core-1.0.5.1 · Snap.Internal.CoreAlternative SnapDefined in snap-core-1.0.5.1 · Snap.Internal.CoreMonadPlus SnapDefined in snap-core-1.0.5.1 · Snap.Internal.CoreMonadIO SnapDefined in snap-core-1.0.5.1 · Snap.Internal.CoreMonadSnap SnapDefined in snap-core-1.0.5.1 · Snap.Internal.CoreMonadBase IO SnapDefined in snap-core-1.0.5.1 · Snap.Internal.CoreMonadBaseControl IO SnapDefined in snap-core-1.0.5.1 · Snap.Internal.Coretype StM Snap a = StSnap aDefined in snap-core-1.0.5.1 · Snap.Internal.Core