-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Another Haskell web framework for rapid development -- @package Spock @version 0.7.12.0 module Web.Spock.Internal.Util data ClientPreferredFormat PrefJSON :: ClientPreferredFormat PrefXML :: ClientPreferredFormat PrefHTML :: ClientPreferredFormat PrefText :: ClientPreferredFormat PrefUnknown :: ClientPreferredFormat mimeMapping :: HashMap Text ClientPreferredFormat detectPreferredFormat :: Text -> ClientPreferredFormat mapReqHeaders :: (ResponseHeaders -> ResponseHeaders) -> Response -> Response instance Show ClientPreferredFormat instance Eq ClientPreferredFormat module Web.Spock.Internal.SessionVault class (Eq (SessionKey s), Hashable (SessionKey s)) => IsSession s where type family SessionKey s :: * getSessionKey :: IsSession s => s -> SessionKey s newtype SessionVault s SessionVault :: Map (SessionKey s) s -> SessionVault s unSessionVault :: SessionVault s -> Map (SessionKey s) s -- | Create a new session vault newSessionVault :: IsSession s => STM (SessionVault s) -- | Load a session loadSession :: IsSession s => SessionKey s -> SessionVault s -> STM (Maybe s) -- | Store a session, overwriting any previous values storeSession :: IsSession s => s -> SessionVault s -> STM () -- | Removea session deleteSession :: IsSession s => SessionKey s -> SessionVault s -> STM () -- | Get all sessions as list toList :: IsSession s => SessionVault s -> STM [s] -- | Remove all sessions that do not match the predicate filterSessions :: IsSession s => (s -> Bool) -> SessionVault s -> STM () instance IsSession (Session conn sess st) module Web.Spock.Shared -- | Run a Spock application. Basically just a wrapper aroung -- Warp.run. runSpock :: Port -> IO Middleware -> IO () -- | Convert a middleware to an application. All failing requests will -- result in a 404 page spockAsApp :: IO Middleware -> IO Application -- | The SpockAction is the monad of all route-actions. You have access to -- the database, session and state of your application. type SpockAction conn sess st = ActionT (WebStateM conn sess st) data ActionT m a -- | Get the original Wai Request object request :: MonadIO m => ActionT m Request -- | Read a header header :: MonadIO m => Text -> ActionT m (Maybe Text) -- | Read a header without converting it to text rawHeader :: MonadIO m => HeaderName -> ActionT m (Maybe ByteString) -- | Read a cookie cookie :: MonadIO m => Text -> ActionT m (Maybe Text) -- | Tries to dected the preferred format of the response using the Accept -- header preferredFormat :: MonadIO m => ActionT m ClientPreferredFormat data ClientPreferredFormat PrefJSON :: ClientPreferredFormat PrefXML :: ClientPreferredFormat PrefHTML :: ClientPreferredFormat PrefText :: ClientPreferredFormat PrefUnknown :: ClientPreferredFormat -- | Get the raw request body body :: MonadIO m => ActionT m ByteString -- | Parse the request body as json jsonBody :: (MonadIO m, FromJSON a) => ActionT m (Maybe a) -- | Parse the request body as json and fails with 500 status code on error jsonBody' :: (MonadIO m, FromJSON a) => ActionT m a -- | Get uploaded files files :: MonadIO m => ActionT m (HashMap Text UploadedFile) data UploadedFile UploadedFile :: !Text -> !Text -> !FilePath -> UploadedFile uf_name :: UploadedFile -> !Text uf_contentType :: UploadedFile -> !Text uf_tempLocation :: UploadedFile -> !FilePath -- | Get all request params params :: MonadIO m => ActionT m [(Text, Text)] -- | Read a request param. Spock looks in route captures first, then in -- POST variables and at last in GET variables param :: (PathPiece p, MonadIO m) => Text -> ActionT m (Maybe p) -- | Like param, but outputs an error when a param is missing param' :: (PathPiece p, MonadIO m) => Text -> ActionT m p -- | Set a response status setStatus :: MonadIO m => Status -> ActionT m () -- | Set a response header. Overwrites already defined headers setHeader :: MonadIO m => Text -> Text -> ActionT m () -- | Redirect to a given url redirect :: MonadIO m => Text -> ActionT m a -- | Abort the current action and jump the next one matching the route jumpNext :: MonadIO m => ActionT m a -- | Set a cookie living for a given number of seconds setCookie :: MonadIO m => Text -> Text -> NominalDiffTime -> ActionT m () -- | Set a cookie living until a specific UTCTime setCookie' :: MonadIO m => Text -> Text -> UTCTime -> ActionT m () deleteCookie :: MonadIO m => Text -> ActionT m () -- | Send a ByteString as response body. Provide your own -- Content-Type bytes :: MonadIO m => ByteString -> ActionT m a -- | Send a lazy ByteString as response body. Provide your own -- Content-Type lazyBytes :: MonadIO m => ByteString -> ActionT m a -- | Send text as a response body. Content-Type will be "text/plain" text :: MonadIO m => Text -> ActionT m a -- | Send a text as response body. Content-Type will be "text/html" html :: MonadIO m => Text -> ActionT m a -- | Send a file as response file :: MonadIO m => Text -> FilePath -> ActionT m a -- | Send json as response. Content-Type will be "application/json" json :: (ToJSON a, MonadIO m) => a -> ActionT m b -- | Use a StreamingBody to generate a response. stream :: MonadIO m => StreamingBody -> ActionT m a -- | Use a custom Response generator as response body. response :: MonadIO m => (Status -> ResponseHeaders -> Response) -> ActionT m a -- | If the Spock application is used as a middleware, you can use this to -- pass request handling to the underlying application. If Spock is not -- uses as a middleware, or there is no underlying application this will -- result in 404 error. middlewarePass :: MonadIO m => ActionT m a -- | Modify the vault (useful for sharing data between middleware and app) modifyVault :: MonadIO m => (Vault -> Vault) -> ActionT m () -- | Query the vault queryVault :: MonadIO m => Key a -> ActionT m (Maybe a) -- | You can feed Spock with either a connection pool, or instructions on -- how to build a connection pool. See ConnBuilder data PoolOrConn a PCPool :: (Pool a) -> PoolOrConn a PCConn :: (ConnBuilder a) -> PoolOrConn a -- | The ConnBuilder instructs Spock how to create or close a database -- connection. data ConnBuilder a ConnBuilder :: IO a -> (a -> IO ()) -> PoolCfg -> ConnBuilder a cb_createConn :: ConnBuilder a -> IO a cb_destroyConn :: ConnBuilder a -> a -> IO () cb_poolConfiguration :: ConnBuilder a -> PoolCfg -- | If Spock should take care of connection pooling, you need to configure -- it depending on what you need. data PoolCfg PoolCfg :: Int -> Int -> NominalDiffTime -> PoolCfg pc_stripes :: PoolCfg -> Int pc_resPerStripe :: PoolCfg -> Int pc_keepOpenTime :: PoolCfg -> NominalDiffTime class HasSpock m where type family SpockConn m :: * type family SpockState m :: * type family SpockSession m :: * runQuery :: HasSpock m => (SpockConn m -> IO a) -> m a getState :: HasSpock m => m (SpockState m) -- | Basic authentification provide a title for the prompt and a function -- to validate user and password. Usage example: -- --
-- get "/my-secret-page" $ -- requireBasicAuth "Secret Page" (\user pass -> return (user == "admin" && pass == "1234")) $ -- do html "This is top secret content. Login using that secret code I provided ;-)" --requireBasicAuth :: MonadIO m => Text -> (Text -> Text -> m Bool) -> ActionT m a -> ActionT m a -- | Session configuration with reasonable defaults defaultSessionCfg :: a -> SessionCfg a -- | Configuration for the session manager data SessionCfg a SessionCfg :: Text -> NominalDiffTime -> Int -> Bool -> a -> Maybe (SessionPersistCfg a) -> NominalDiffTime -> SessionCfg a -- | name of the client side cookie sc_cookieName :: SessionCfg a -> Text -- | how long shoud a client session live sc_sessionTTL :: SessionCfg a -> NominalDiffTime -- | entropy of the session id sent to the client sc_sessionIdEntropy :: SessionCfg a -> Int -- | if this is true, every page reload will renew the session time to live -- counter sc_sessionExpandTTL :: SessionCfg a -> Bool -- | initial session for visitors sc_emptySession :: SessionCfg a -> a -- | persistence interface for sessions sc_persistCfg :: SessionCfg a -> Maybe (SessionPersistCfg a) -- | how often should the session manager check for dangeling dead sessions sc_housekeepingInterval :: SessionCfg a -> NominalDiffTime data SessionPersistCfg a SessionPersistCfg :: IO [(SessionId, UTCTime, a)] -> ([(SessionId, UTCTime, a)] -> IO ()) -> SessionPersistCfg a spc_load :: SessionPersistCfg a -> IO [(SessionId, UTCTime, a)] spc_store :: SessionPersistCfg a -> [(SessionId, UTCTime, a)] -> IO () -- | Simple session persisting configuration. DO NOT USE IN PRODUCTION readShowSessionPersist :: (Read a, Show a) => FilePath -> SessionPersistCfg a type SessionId = Text -- | Get the current users sessionId. Note that this ID should only be -- shown to it's owner as otherwise sessions can be hijacked. getSessionId :: SpockAction conn sess st SessionId -- | Read the stored session readSession :: SpockAction conn sess st sess -- | Write to the current session. Note that all data is stored on the -- server. The user only reciedes a sessionId to be identified. writeSession :: sess -> SpockAction conn sess st () -- | Modify the stored session modifySession :: (sess -> sess) -> SpockAction conn sess st () -- | Modify the stored session and return a value modifySession' :: (sess -> (sess, a)) -> SpockAction conn sess st a -- | Modify the stored session and return the new value after modification modifyReadSession :: (sess -> sess) -> SpockAction conn sess st sess -- | Globally delete all existing sessions. This is useful for example if -- you want to require all users to relogin clearAllSessions :: SpockAction conn sess st () -- | Read the heart of Spock. This is useful if you want to construct your -- own monads that work with runQuery and getState using "runSpockIO" getSpockHeart :: MonadTrans t => t (WebStateM conn sess st) (WebState conn sess st) -- | Run an action inside of Spocks core monad. This allows you to use -- runQuery and getState runSpockIO :: WebState conn sess st -> WebStateM conn sess st a -> IO a type WebStateM conn sess st = WebStateT conn sess st (ResourceT IO) data WebState conn sess st -- | Since version 0.7 Spock features a new routing system that enables -- more type-safe code while still being relatively simple and -- lightweight. You should consider using that (see -- Web.Spock.Safe) instead of this module. This module is not yet -- deprecated, but this may happen anytime soon. module Web.Spock.Simple -- | Create a spock application using a given db storageLayer and an -- initial state. Spock works with database libraries that already -- implement connection pooling and with those that don't come with it -- out of the box. For more see the PoolOrConn type. Use -- runSpock to run the app or spockAsApp to create a -- Wai.Application spock :: SessionCfg sess -> PoolOrConn conn -> st -> SpockM conn sess st () -> IO Middleware type SpockM conn sess st a = SpockT (WebStateM conn sess st) a -- | Create a raw spock application with custom underlying monad Use -- runSpock to run the app or spockAsApp to create a -- Wai.Application spockT :: MonadIO m => (forall a. m a -> IO a) -> SpockT m () -> IO Middleware data SpockT m a data SpockRoute -- | Combine two route components safely -- --
-- >>> "/foo" <//> "/bar" -- "/foo/bar" ---- --
-- >>> "foo" <//> "bar" -- "/foo/bar" ---- --
-- >>> "foo <//> "/bar" -- "/foo/bar" --(/>) :: SpockRoute -> SpockRoute -> SpockRoute -- | Define a subcomponent. Usage example: -- --
-- subcomponent "site" $
-- do get "home" homeHandler
-- get ("misc" <//> ":param") $ -- ...
-- subcomponent "/admin" $
-- get "home" adminHomeHandler
--
--
-- The request /site/home will be routed to homeHandler and the request
-- /admin/home will be routed to adminHomeHandler
subcomponent :: Monad m => SpockRoute -> SpockT m () -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb GET and
-- the given route match
get :: MonadIO m => SpockRoute -> ActionT m () -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb POST and
-- the given route match
post :: MonadIO m => SpockRoute -> ActionT m () -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb 'GET'/'POST' and
-- the given route match
getpost :: MonadIO m => SpockRoute -> ActionT m () -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb HEAD and
-- the given route match
head :: MonadIO m => SpockRoute -> ActionT m () -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb PUT and
-- the given route match
put :: MonadIO m => SpockRoute -> ActionT m () -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb DELETE
-- and the given route match
delete :: MonadIO m => SpockRoute -> ActionT m () -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb PATCH and
-- the given route match
patch :: MonadIO m => SpockRoute -> ActionT m () -> SpockT m ()
-- | Specify an action that will be run when a HTTP verb and the given
-- route match
hookRoute :: Monad m => StdMethod -> SpockRoute -> ActionT m () -> SpockT m ()
-- | Specify an action that will be run when a HTTP verb matches but no
-- defined route matches. The full path is passed as an argument
hookAny :: Monad m => StdMethod -> ([Text] -> ActionT m ()) -> SpockT m ()
-- | HTTP standard method (as defined by RFC 2616, and PATCH which is
-- defined by RFC 5789).
data StdMethod :: *
GET :: StdMethod
POST :: StdMethod
HEAD :: StdMethod
PUT :: StdMethod
DELETE :: StdMethod
TRACE :: StdMethod
CONNECT :: StdMethod
OPTIONS :: StdMethod
PATCH :: StdMethod
-- | Hook wai middleware into Spock
middleware :: Monad m => Middleware -> SpockT m ()
-- | SafeActions are actions that need to be protected from csrf attacks
class (Hashable a, Eq a, Typeable a) => SafeAction conn sess st a
runSafeAction :: SafeAction conn sess st a => a -> SpockAction conn sess st ()
-- | Wire up a safe action: Safe actions are actions that are protected
-- from csrf attacks. Here's a usage example:
--
--
-- newtype DeleteUser = DeleteUser Int deriving (Hashable, Typeable, Eq)
--
-- instance SafeAction Connection () () DeleteUser where
-- runSafeAction (DeleteUser i) =
-- do runQuery $ deleteUserFromDb i
-- redirect "/user-list"
--
-- get ("user-details" <//> ":userId") $
-- do userId <- param' "userId"
-- deleteUrl <- safeActionPath (DeleteUser userId)
-- html $ "Click <a href='" <> deleteUrl <> "'>here</a> to delete user!"
--
--
-- Note that safeActions currently only support GET and POST requests.
safeActionPath :: (SafeAction conn sess st a, HasSpock (SpockAction conn sess st), SpockConn (SpockAction conn sess st) ~ conn, SpockSession (SpockAction conn sess st) ~ sess, SpockState (SpockAction conn sess st) ~ st) => a -> SpockAction conn sess st Text
instance Monad m => Monad (SpockT m)
instance Functor m => Functor (SpockT m)
instance (Monad m, Functor m) => Applicative (SpockT m)
instance MonadIO m => MonadIO (SpockT m)
instance Eq SpockRoute
instance Ord SpockRoute
instance Show SpockRoute
instance Read SpockRoute
instance IsString SpockRoute
instance MonadTrans SpockT
-- | This module implements the type safe routing aproach. It should be
-- used by all new Spock powered applications. To learn more about the
-- routing, read the corresponding blog post available at
-- http://www.spock.li/2015/04/19/type-safe_routing.html
module Web.Spock.Safe
-- | Create a spock application using a given db storageLayer and an
-- initial state. Spock works with database libraries that already
-- implement connection pooling and with those that don't come with it
-- out of the box. For more see the PoolOrConn type. Use
-- runSpock to run the app or spockAsApp to create a
-- Wai.Application
spock :: SessionCfg sess -> PoolOrConn conn -> st -> SpockM conn sess st () -> IO Middleware
type SpockM conn sess st a = SpockT (WebStateM conn sess st) a
-- | Create a raw spock application with custom underlying monad Use
-- runSpock to run the app or spockAsApp to create a
-- Wai.Application
spockT :: MonadIO m => (forall a. m a -> IO a) -> SpockT m () -> IO Middleware
data SpockT m a
data Path (as :: [*]) :: [*] -> *
-- | The root of a path piece. Use to define a handler for "/"
root :: Path ([] *)
type Var a = Path ((:) * a ([] *))
-- | A route parameter
var :: (Typeable * a, PathPiece a) => Path ((:) * a ([] *))
-- | A static route piece
static :: String -> Path ([] *)
-- | Combine two path components
(/>) :: Path as -> Path bs -> Path (Append as bs)
-- | Render a route applying path pieces
renderRoute :: Path as -> HVectElim as Text
-- | Define a subcomponent. Usage example:
--
--
-- subcomponent "site" $
-- do get "home" homeHandler
-- get ("misc" <//> var) $ -- ...
-- subcomponent "admin" $
-- do get "home" adminHomeHandler
--
--
-- The request /site/home will be routed to homeHandler and the request
-- /admin/home will be routed to adminHomeHandler
subcomponent :: Monad m => Path [] -> SpockT m () -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb GET and
-- the given route match
get :: MonadIO m => Path xs -> HVectElim xs (ActionT m ()) -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb POST and
-- the given route match
post :: MonadIO m => Path xs -> HVectElim xs (ActionT m ()) -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb 'GET'/'POST' and
-- the given route match
getpost :: MonadIO m => Path xs -> HVectElim xs (ActionT m ()) -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb HEAD and
-- the given route match
head :: MonadIO m => Path xs -> HVectElim xs (ActionT m ()) -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb PUT and
-- the given route match
put :: MonadIO m => Path xs -> HVectElim xs (ActionT m ()) -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb DELETE
-- and the given route match
delete :: MonadIO m => Path xs -> HVectElim xs (ActionT m ()) -> SpockT m ()
-- | Specify an action that will be run when the HTTP verb PATCH and
-- the given route match
patch :: MonadIO m => Path xs -> HVectElim xs (ActionT m ()) -> SpockT m ()
-- | Specify an action that will be run when a HTTP verb and the given
-- route match
hookRoute :: Monad m => StdMethod -> Path xs -> HVectElim xs (ActionT m ()) -> SpockT m ()
-- | Specify an action that will be run when a HTTP verb matches but no
-- defined route matches. The full path is passed as an argument
hookAny :: Monad m => StdMethod -> ([Text] -> ActionT m ()) -> SpockT m ()
-- | HTTP standard method (as defined by RFC 2616, and PATCH which is
-- defined by RFC 5789).
data StdMethod :: *
GET :: StdMethod
POST :: StdMethod
HEAD :: StdMethod
PUT :: StdMethod
DELETE :: StdMethod
TRACE :: StdMethod
CONNECT :: StdMethod
OPTIONS :: StdMethod
PATCH :: StdMethod
-- | Hook wai middleware into Spock
middleware :: Monad m => Middleware -> SpockT m ()
-- | SafeActions are actions that need to be protected from csrf attacks
class (Hashable a, Eq a, Typeable a) => SafeAction conn sess st a
runSafeAction :: SafeAction conn sess st a => a -> SpockAction conn sess st ()
-- | Wire up a safe action: Safe actions are actions that are protected
-- from csrf attacks. Here's a usage example:
--
--
-- newtype DeleteUser = DeleteUser Int deriving (Hashable, Typeable, Eq)
--
-- instance SafeAction Connection () () DeleteUser where
-- runSafeAction (DeleteUser i) =
-- do runQuery $ deleteUserFromDb i
-- redirect "/user-list"
--
-- get ("user-details" <//> var) $ \userId ->
-- do deleteUrl <- safeActionPath (DeleteUser userId)
-- html $ "Click <a href='" <> deleteUrl <> "'>here</a> to delete user!"
--
--
-- Note that safeActions currently only support GET and POST requests.
safeActionPath :: (SafeAction conn sess st a, HasSpock (SpockAction conn sess st), SpockConn (SpockAction conn sess st) ~ conn, SpockSession (SpockAction conn sess st) ~ sess, SpockState (SpockAction conn sess st) ~ st) => a -> SpockAction conn sess st Text
instance Monad m => Monad (SpockT m)
instance Functor m => Functor (SpockT m)
instance (Monad m, Functor m) => Applicative (SpockT m)
instance MonadIO m => MonadIO (SpockT m)
instance MonadTrans SpockT
-- | This module reexports type safe routing aproach which should be the
-- default for all Spock applications. To learn more about the routing,
-- read the corresponding blog post available at
-- http://www.spock.li/2015/04/19/type-safe_routing.html
module Web.Spock