Spock-0.14.0.1: Another Haskell web framework for rapid development
Safe HaskellNone
LanguageHaskell2010

Web.Spock

Synopsis

Launching Spock

runSpock :: Port -> IO Middleware -> IO () #

Run a Spock application. Basically just a wrapper around run.

runSpockNoBanner :: Port -> IO Middleware -> IO () #

Like runSpock, but does not display the banner "Spock is running on port XXX" on stdout.

spockAsApp :: IO Middleware -> IO Application #

Convert a middleware to an application. All failing requests will result in a 404 page

Spock's route definition monad

spock :: forall conn sess st. SpockCfg conn sess st -> SpockM conn sess st () -> IO Middleware Source #

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

type SpockM conn sess st = SpockCtxM () conn sess st Source #

type SpockCtxM ctx conn sess st = SpockCtxT ctx (WebStateM conn sess st) Source #

Defining routes

data Path (as :: [Type]) (pathState :: PathState) #

Instances

Instances details
(a ~ ('[] :: [Type]), pathState ~ 'Open) => IsString (Path a pathState) 
Instance details

Defined in Web.Routing.Combinators

Methods

fromString :: String -> Path a pathState #

root :: Path ('[] :: [Type]) 'Open #

The root of a path piece. Use to define a handler for "/"

type Var a = Path '[a] 'Open #

var :: (Typeable a, FromHttpApiData a) => Path '[a] 'Open #

A route parameter

static :: String -> Path ('[] :: [Type]) 'Open #

A static route piece

(<//>) :: forall (as :: [Type]) (bs :: [Type]) (ps :: PathState). Path as 'Open -> Path bs ps -> Path (Append as bs) ps #

Combine two path components

wildcard :: Path '[Text] 'Closed #

Matches the rest of the route. Should be the last part of the path.

Rendering routes

renderRoute :: forall (as :: [Type]). AllHave ToHttpApiData as => Path as 'Open -> HVectElim as Text #

Render a route applying path pieces

Hooking routes

prehook :: forall t (m :: Type -> Type) ctx ctx'. (RouteM t, MonadIO m) => ActionCtxT ctx m ctx' -> t ctx' m () -> t ctx m () #

Specify an action that will be run before all subroutes. It can modify the requests current context

type RouteSpec xs ps ctx conn sess st = Path xs ps -> HVectElim xs (SpockActionCtx ctx conn sess st ()) -> SpockCtxM ctx conn sess st () Source #

get :: HasRep xs => RouteSpec xs ps ctx conn sess st Source #

Specify an action that will be run when the HTTP verb GET and the given route match

post :: HasRep xs => RouteSpec xs ps ctx conn sess st Source #

Specify an action that will be run when the HTTP verb POST and the given route match

getpost :: HasRep xs => RouteSpec xs ps ctx conn sess st Source #

Specify an action that will be run when the HTTP verb GET/POST and the given route match

head :: HasRep xs => RouteSpec xs ps ctx conn sess st Source #

Specify an action that will be run when the HTTP verb HEAD and the given route match

put :: HasRep xs => RouteSpec xs ps ctx conn sess st Source #

Specify an action that will be run when the HTTP verb PUT and the given route match

delete :: HasRep xs => RouteSpec xs ps ctx conn sess st Source #

Specify an action that will be run when the HTTP verb DELETE and the given route match

patch :: HasRep xs => RouteSpec xs ps ctx conn sess st Source #

Specify an action that will be run when the HTTP verb PATCH and the given route match

hookRoute :: HasRep xs => StdMethod -> RouteSpec xs ps ctx conn sess st Source #

Specify an action that will be run when a standard HTTP verb and the given route match

hookRouteCustom :: HasRep xs => Text -> RouteSpec xs ps ctx conn sess st Source #

Specify an action that will be run when a custom HTTP verb and the given route match

hookAny :: StdMethod -> ([Text] -> SpockActionCtx ctx conn sess st ()) -> SpockCtxM ctx conn sess st () Source #

Specify an action that will be run when a standard HTTP verb matches but no defined route matches. The full path is passed as an argument

hookAnyCustom :: Text -> ([Text] -> SpockActionCtx ctx conn sess st ()) -> SpockCtxM ctx conn sess st () Source #

Specify an action that will be run when a custom HTTP verb matches but no defined route matches. The full path is passed as an argument

hookRouteAll :: HasRep xs => RouteSpec xs ps ctx conn sess st Source #

Specify an action that will be run regardless of the HTTP verb

hookAnyAll :: ([Text] -> SpockActionCtx ctx conn sess st ()) -> SpockCtxM ctx conn sess st () Source #

Specify an action that will be run regardless of the HTTP verb and no defined route matches. The full path is passed as an argument

data StdMethod #

HTTP standard method (as defined by RFC 2616, and PATCH which is defined by RFC 5789).

Constructors

GET 
POST 
HEAD 
PUT 
DELETE 
TRACE 
CONNECT 
OPTIONS 
PATCH 

Instances

Instances details
Bounded StdMethod 
Instance details

Defined in Network.HTTP.Types.Method

Enum StdMethod 
Instance details

Defined in Network.HTTP.Types.Method

Eq StdMethod 
Instance details

Defined in Network.HTTP.Types.Method

Ord StdMethod 
Instance details

Defined in Network.HTTP.Types.Method

Read StdMethod 
Instance details

Defined in Network.HTTP.Types.Method

Show StdMethod 
Instance details

Defined in Network.HTTP.Types.Method

Ix StdMethod 
Instance details

Defined in Network.HTTP.Types.Method

Adding Wai.Middleware

middleware :: forall t (m :: Type -> Type) ctx. (RouteM t, Monad m) => Middleware -> t ctx m () #

Hook wai middleware into Spock

Actions

type SpockAction conn sess st = SpockActionCtx () conn sess st Source #

The SpockAction is a specialisation of SpockActionCtx with a () context.

type SpockActionCtx ctx conn sess st = ActionCtxT ctx (WebStateM conn sess st) Source #

The SpockActionCtx is the monad of all route-actions. You have access to the context of the request and database, session and state of your application.

class HasSpock m where Source #

Associated Types

type SpockConn m :: * Source #

type SpockState m :: * Source #

type SpockSession m :: * Source #

Methods

runQuery :: (SpockConn m -> IO a) -> m a Source #

Give you access to a database connectin from the connection pool. The connection is released back to the pool once the function terminates.

getState :: m (SpockState m) Source #

Read the application's state. If you wish to have mutable state, you could use a TVar from the STM packge.

getSessMgr :: m (SpockSessionManager (SpockConn m) (SpockSession m) (SpockState m)) Source #

Get the session manager

getSpockCfg :: m (SpockCfg (SpockConn m) (SpockSession m) (SpockState m)) Source #

Get the Spock configuration

Instances

Instances details
MonadTrans t => HasSpock (t (WebStateM conn sess st)) Source # 
Instance details

Defined in Web.Spock.Internal.Monad

Associated Types

type SpockConn (t (WebStateM conn sess st)) Source #

type SpockState (t (WebStateM conn sess st)) Source #

type SpockSession (t (WebStateM conn sess st)) Source #

Methods

runQuery :: (SpockConn (t (WebStateM conn sess st)) -> IO a) -> t (WebStateM conn sess st) a Source #

getState :: t (WebStateM conn sess st) (SpockState (t (WebStateM conn sess st))) Source #

getSessMgr :: t (WebStateM conn sess st) (SpockSessionManager (SpockConn (t (WebStateM conn sess st))) (SpockSession (t (WebStateM conn sess st))) (SpockState (t (WebStateM conn sess st)))) Source #

getSpockCfg :: t (WebStateM conn sess st) (SpockCfg (SpockConn (t (WebStateM conn sess st))) (SpockSession (t (WebStateM conn sess st))) (SpockState (t (WebStateM conn sess st)))) Source #

HasSpock (WebStateM conn sess st) Source # 
Instance details

Defined in Web.Spock.Internal.Monad

Associated Types

type SpockConn (WebStateM conn sess st) Source #

type SpockState (WebStateM conn sess st) Source #

type SpockSession (WebStateM conn sess st) Source #

Methods

runQuery :: (SpockConn (WebStateM conn sess st) -> IO a) -> WebStateM conn sess st a Source #

getState :: WebStateM conn sess st (SpockState (WebStateM conn sess st)) Source #

getSessMgr :: WebStateM conn sess st (SpockSessionManager (SpockConn (WebStateM conn sess st)) (SpockSession (WebStateM conn sess st)) (SpockState (WebStateM conn sess st))) Source #

getSpockCfg :: WebStateM conn sess st (SpockCfg (SpockConn (WebStateM conn sess st)) (SpockSession (WebStateM conn sess st)) (SpockState (WebStateM conn sess st))) Source #

data SessionManager m conn sess st Source #

getCsrfToken :: SpockActionCtx ctx conn sess st Text Source #

Get the CSRF token for the current user. This token must be sent on all non GET requests via a post parameter or HTTP-Header if spc_csrfProtection is turned on. See configuration SpockCfg documentation for more information

getClientCsrfToken :: SpockActionCtx ctx conn sess st (Maybe Text) Source #

Get the CSRF token sent by the client. You should not need to call this manually if spc_csrfProtection is turned on.

csrfCheck :: SpockActionCtx ctx conn sess st () Source #

Check that the client sent a valid CSRF token. You should not need to call this manually in non GET requests if spc_csrfProtection is turned on.

Accessing internals

type WebStateM conn sess st = WebStateT conn sess st (ResourceT IO) Source #

data WebStateT conn sess st m a Source #

Instances

Instances details
MonadBaseControl b m => MonadBaseControl b (WebStateT conn sess st m) Source # 
Instance details

Defined in Web.Spock.Internal.Types

Associated Types

type StM (WebStateT conn sess st m) a #

Methods

liftBaseWith :: (RunInBase (WebStateT conn sess st m) b -> b a) -> WebStateT conn sess st m a #

restoreM :: StM (WebStateT conn sess st m) a -> WebStateT conn sess st m a #

MonadBase b m => MonadBase b (WebStateT conn sess st m) Source # 
Instance details

Defined in Web.Spock.Internal.Types

Methods

liftBase :: b α -> WebStateT conn sess st m α #

MonadTrans t => HasSpock (t (WebStateM conn sess st)) Source # 
Instance details

Defined in Web.Spock.Internal.Monad

Associated Types

type SpockConn (t (WebStateM conn sess st)) Source #

type SpockState (t (WebStateM conn sess st)) Source #

type SpockSession (t (WebStateM conn sess st)) Source #

Methods

runQuery :: (SpockConn (t (WebStateM conn sess st)) -> IO a) -> t (WebStateM conn sess st) a Source #

getState :: t (WebStateM conn sess st) (SpockState (t (WebStateM conn sess st))) Source #

getSessMgr :: t (WebStateM conn sess st) (SpockSessionManager (SpockConn (t (WebStateM conn sess st))) (SpockSession (t (WebStateM conn sess st))) (SpockState (t (WebStateM conn sess st)))) Source #

getSpockCfg :: t (WebStateM conn sess st) (SpockCfg (SpockConn (t (WebStateM conn sess st))) (SpockSession (t (WebStateM conn sess st))) (SpockState (t (WebStateM conn sess st)))) Source #

MonadTrans (WebStateT conn sess st) Source # 
Instance details

Defined in Web.Spock.Internal.Types

Methods

lift :: Monad m => m a -> WebStateT conn sess st m a #

MonadTransControl (WebStateT conn sess st) Source # 
Instance details

Defined in Web.Spock.Internal.Types

Associated Types

type StT (WebStateT conn sess st) a #

Methods

liftWith :: Monad m => (Run (WebStateT conn sess st) -> m a) -> WebStateT conn sess st m a #

restoreT :: Monad m => m (StT (WebStateT conn sess st) a) -> WebStateT conn sess st m a #

HasSpock (WebStateM conn sess st) Source # 
Instance details

Defined in Web.Spock.Internal.Monad

Associated Types

type SpockConn (WebStateM conn sess st) Source #

type SpockState (WebStateM conn sess st) Source #

type SpockSession (WebStateM conn sess st) Source #

Methods

runQuery :: (SpockConn (WebStateM conn sess st) -> IO a) -> WebStateM conn sess st a Source #

getState :: WebStateM conn sess st (SpockState (WebStateM conn sess st)) Source #

getSessMgr :: WebStateM conn sess st (SpockSessionManager (SpockConn (WebStateM conn sess st)) (SpockSession (WebStateM conn sess st)) (SpockState (WebStateM conn sess st))) Source #

getSpockCfg :: WebStateM conn sess st (SpockCfg (SpockConn (WebStateM conn sess st)) (SpockSession (WebStateM conn sess st)) (SpockState (WebStateM conn sess st))) Source #

Monad m => MonadReader (WebState conn sess st) (WebStateT conn sess st m) Source # 
Instance details

Defined in Web.Spock.Internal.Types

Methods

ask :: WebStateT conn sess st m (WebState conn sess st) #

local :: (WebState conn sess st -> WebState conn sess st) -> WebStateT conn sess st m a -> WebStateT conn sess st m a #

reader :: (WebState conn sess st -> a) -> WebStateT conn sess st m a #

Monad m => Monad (WebStateT conn sess st m) Source # 
Instance details

Defined in Web.Spock.Internal.Types

Methods

(>>=) :: WebStateT conn sess st m a -> (a -> WebStateT conn sess st m b) -> WebStateT conn sess st m b #

(>>) :: WebStateT conn sess st m a -> WebStateT conn sess st m b -> WebStateT conn sess st m b #

return :: a -> WebStateT conn sess st m a #

Functor m => Functor (WebStateT conn sess st m) Source # 
Instance details

Defined in Web.Spock.Internal.Types

Methods

fmap :: (a -> b) -> WebStateT conn sess st m a -> WebStateT conn sess st m b #

(<$) :: a -> WebStateT conn sess st m b -> WebStateT conn sess st m a #

Applicative m => Applicative (WebStateT conn sess st m) Source # 
Instance details

Defined in Web.Spock.Internal.Types

Methods

pure :: a -> WebStateT conn sess st m a #

(<*>) :: WebStateT conn sess st m (a -> b) -> WebStateT conn sess st m a -> WebStateT conn sess st m b #

liftA2 :: (a -> b -> c) -> WebStateT conn sess st m a -> WebStateT conn sess st m b -> WebStateT conn sess st m c #

(*>) :: WebStateT conn sess st m a -> WebStateT conn sess st m b -> WebStateT conn sess st m b #

(<*) :: WebStateT conn sess st m a -> WebStateT conn sess st m b -> WebStateT conn sess st m a #

MonadIO m => MonadIO (WebStateT conn sess st m) Source # 
Instance details

Defined in Web.Spock.Internal.Types

Methods

liftIO :: IO a -> WebStateT conn sess st m a #

type SpockConn (t (WebStateM conn sess st)) Source # 
Instance details

Defined in Web.Spock.Internal.Monad

type SpockConn (t (WebStateM conn sess st)) = conn
type SpockState (t (WebStateM conn sess st)) Source # 
Instance details

Defined in Web.Spock.Internal.Monad

type SpockState (t (WebStateM conn sess st)) = st
type SpockSession (t (WebStateM conn sess st)) Source # 
Instance details

Defined in Web.Spock.Internal.Monad

type SpockSession (t (WebStateM conn sess st)) = sess
type SpockConn (WebStateM conn sess st) Source # 
Instance details

Defined in Web.Spock.Internal.Monad

type SpockConn (WebStateM conn sess st) = conn
type SpockState (WebStateM conn sess st) Source # 
Instance details

Defined in Web.Spock.Internal.Monad

type SpockState (WebStateM conn sess st) = st
type SpockSession (WebStateM conn sess st) Source # 
Instance details

Defined in Web.Spock.Internal.Monad

type SpockSession (WebStateM conn sess st) = sess
type StT (WebStateT conn sess st) a Source # 
Instance details

Defined in Web.Spock.Internal.Types

type StT (WebStateT conn sess st) a = a
type StM (WebStateT conn sess st m) a Source # 
Instance details

Defined in Web.Spock.Internal.Types

type StM (WebStateT conn sess st m) a = ComposeSt (WebStateT conn sess st) m a

data WebState conn sess st Source #

Instances

Instances details
Monad m => MonadReader (WebState conn sess st) (WebStateT conn sess st m) Source # 
Instance details

Defined in Web.Spock.Internal.Types

Methods

ask :: WebStateT conn sess st m (WebState conn sess st) #

local :: (WebState conn sess st -> WebState conn sess st) -> WebStateT conn sess st m a -> WebStateT conn sess st m a #

reader :: (WebState conn sess st -> a) -> WebStateT conn sess st m a #

getSpockHeart :: MonadTrans t => t (WebStateM conn sess st) (WebState conn sess st) Source #

Read the heart of Spock. This is useful if you want to construct your own monads that work with runQuery and getState using runSpockIO

runSpockIO :: WebState conn sess st -> WebStateM conn sess st a -> IO a Source #

Run an action inside of Spocks core monad. This allows you to use runQuery and getState

getSpockPool :: MonadTrans t => t (WebStateM conn sess st) (Pool conn) Source #

Read the connection pool of Spock. This is useful if you want to construct your own monads that work with runQuery and getState using runSpockIO