Spock-0.11.0.0: Another Haskell web framework for rapid development

Safe HaskellNone
LanguageHaskell98

Web.Spock

Contents

Synopsis

Lauching 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) 

root :: Path ([] *) Open

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

type Var a = Path ((:) * a ([] *)) Open

var :: (Typeable * a, PathPiece 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

Rendering routes

renderRoute :: Path as Open -> HVectElim as Text

Render a route applying path pieces

Hooking routes

subcomponent :: Monad m => Path ([] *) Open -> SpockCtxT ctx m () -> SpockCtxT 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 :: MonadIO m => ActionCtxT ctx m ctx' -> SpockCtxT ctx' m () -> SpockCtxT 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 :: Monad m => Middleware -> SpockCtxT 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

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 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 
MonadBaseControl b m => MonadBaseControl b (WebStateT conn sess st m) Source 
MonadTrans (WebStateT conn sess st) Source 
MonadTransControl (WebStateT conn sess st) Source 
Monad m => MonadReader (WebState conn sess st) (WebStateT conn sess st m) Source 
Monad m => Monad (WebStateT conn sess st m) Source 
Functor m => Functor (WebStateT conn sess st m) Source 
Applicative m => Applicative (WebStateT conn sess st m) Source 
MonadIO m => MonadIO (WebStateT conn sess st m) Source 
type SpockConn (t (WebStateM conn sess st)) = conn Source 
type SpockState (t (WebStateM conn sess st)) = st Source 
type SpockSession (t (WebStateM conn sess st)) = sess Source 
type SpockConn (WebStateM conn sess st) = conn Source 
type SpockState (WebStateM conn sess st) = st Source 
type SpockSession (WebStateM conn sess st) = sess Source 
type StT (WebStateT conn sess st) a = a Source 
type StM (WebStateT conn sess st m) a = ComposeSt (WebStateT conn sess st) m a Source 

data WebState conn sess st Source

Instances

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

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