growler-0.3.2: A revised version of the scotty library that attempts to be simpler and more performant.

Safe HaskellNone
LanguageHaskell2010

Web.Growler

Contents

Synopsis

Running a growler app

growl :: MonadIO m => (forall a. m a -> IO a) -> HandlerT m () -> GrowlerT m () -> IO () Source

growler :: MonadIO m => (forall a. m a -> IO a) -> HandlerT m () -> GrowlerT m () -> IO Application Source

Routing

data GrowlerT m a Source

Instances

get :: MonadIO m => RoutePattern -> HandlerT m () -> GrowlerT m () Source

get = addroute GET

post :: MonadIO m => RoutePattern -> HandlerT m () -> GrowlerT m () Source

post = addroute POST

put :: MonadIO m => RoutePattern -> HandlerT m () -> GrowlerT m () Source

put = addroute PUT

delete :: MonadIO m => RoutePattern -> HandlerT m () -> GrowlerT m () Source

delete = addroute DELETE

patch :: MonadIO m => RoutePattern -> HandlerT m () -> GrowlerT m () Source

patch = addroute PATCH

matchAny :: MonadIO m => RoutePattern -> HandlerT m () -> GrowlerT m () Source

Add a route that matches regardless of the HTTP verb.

notFound :: MonadIO m => HandlerT m () Source

Specify an action to take if nothing else is found. Note: this _always_ matches, so should generally be the last route specified.

addRoute :: MonadIO m => StdMethod -> RoutePattern -> HandlerT m () -> GrowlerT m () Source

Define a route with a StdMethod, Text value representing the path spec, and a body (Action) which modifies the response.

addroute GET "/" $ text "beam me up!"

The path spec can include values starting with a colon, which are interpreted as captures. These are named wildcards that can be looked up with param.

addroute GET "/foo/:bar" $ do
    v <- param "bar"
    text v
>>> curl http://localhost:3000/foo/something
something

regex :: String -> RoutePattern Source

Match requests using a regular expression. Named captures are not yet supported.

get (regex "^/f(.*)r$") $ do
   path <- param "0"
   cap <- param "1"
   text $ mconcat ["Path: ", path, "\nCapture: ", cap]
>>> curl http://localhost:3000/foo/bar
Path: /foo/bar
Capture: oo/ba

capture :: String -> RoutePattern Source

Standard Sinatra-style route. Named captures are prepended with colons. This is the default route type generated by OverloadedString routes. i.e.

get (capture "/foo/:bar") $ ...

and

{-# LANGUAGE OverloadedStrings #-}
...
get "/foo/:bar" $ ...

are equivalent.

function :: (Request -> Text) -> (Request -> MatchResult) -> RoutePattern Source

Build a route based on a function which can match using the entire Request object. Nothing indicates the route does not match. A Just value indicates a successful match, optionally returning a list of key-value pairs accessible by param.

get (function $ \req -> Just [("version", T.pack $ show $ httpVersion req)]) $ do
    v <- param "version"
    text v
>>> curl http://localhost:3000/
HTTP/1.1

literal :: String -> RoutePattern Source

Build a route that requires the requested path match exactly, without captures.

handlerHook :: Monad m => (HandlerT m () -> HandlerT m ()) -> GrowlerT m () Source

Handlers

html :: Monad m => Text -> HandlerT m () Source

json :: Monad m => ToJSON a => a -> HandlerT m () Source

text :: Monad m => Text -> HandlerT m () Source

Parsable

class Parsable a where Source

Minimum implemention: parseParam

Minimal complete definition

parseParam

Methods

parseParam :: ByteString -> Either ByteString a Source

Take a ByteString value and parse it as a, or fail with a message.

parseParamList :: ByteString -> Either ByteString [a] Source

Default implementation parses comma-delimited lists.

parseParamList t = mapM parseParam (BS.split ',' t)

Instances

Parsable Bool 
Parsable Char

Overrides default parseParamList to parse String.

Parsable Double 
Parsable Float 
Parsable Int 
Parsable Integer 
Parsable ()

Checks if parameter is present and is null-valued, not a literal '()'. If the URI requested is: '/foo?bar=()&baz' then baz will parse as (), where bar will not.

Parsable ByteString 
Parsable Text 
Parsable Text 
Parsable a => Parsable [a] 

readEither :: Read a => ByteString -> Either ByteString a Source

Useful for creating Parsable instances for things that already implement Read. Ex:

instance Parsable Int where parseParam = readEither

Internals