Spock-0.12.0.1: Another Haskell web framework for rapid development

Safe HaskellNone
LanguageHaskell98

Web.Spock

Contents

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 pathState :: [*] -> PathState -> * #

Instances

((~) [*] a ([] *), (~) PathState pathState Open) => IsString (Path a pathState) 

Methods

fromString :: String -> Path a pathState #

root :: Path ([] *) 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 ([] *) Open #

A static route piece

(<//>) :: 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 :: AllHave ToHttpApiData as => Path as Open -> HVectElim as Text #

Render a route applying path pieces

Hooking routes

subcomponent :: (RouteM t, Monad m) => Path ([] *) Open -> t ctx m () -> t ctx m () #

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

prehook :: (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

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 

Adding Wai.Middleware

middleware :: (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 #

Minimal complete definition

runQuery, getState, getSessMgr, getSpockCfg

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

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

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

Methods

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

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

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 #

MonadTrans (WebStateT conn sess st) Source # 

Methods

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

MonadTransControl (WebStateT conn sess st) Source # 

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 #

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

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 # 

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 #

fail :: String -> WebStateT conn sess st m a #

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

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 # 

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 #

(*>) :: 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 # 

Methods

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

type SpockConn (t (WebStateM conn sess st)) Source # 
type SpockConn (t (WebStateM conn sess st)) = conn
type SpockState (t (WebStateM conn sess st)) Source # 
type SpockState (t (WebStateM conn sess st)) = st
type SpockSession (t (WebStateM conn sess st)) Source # 
type SpockSession (t (WebStateM conn sess st)) = sess
type SpockConn (WebStateM conn sess st) Source # 
type SpockConn (WebStateM conn sess st) = conn
type SpockState (WebStateM conn sess st) Source # 
type SpockState (WebStateM conn sess st) = st
type SpockSession (WebStateM conn sess st) Source # 
type SpockSession (WebStateM conn sess st) = sess
type StT (WebStateT conn sess st) a Source # 
type StT (WebStateT conn sess st) a = a
type StM (WebStateT conn sess st m) a Source # 
type StM (WebStateT conn sess st m) a = ComposeSt (WebStateT conn sess st) m a

data WebState conn sess st Source #

Instances

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

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