Safe Haskell | None |
---|
It should be noted that most of the code snippets below depend on the OverloadedStrings language pragma.
The functions in this module allow an arbitrary monad to be embedded in Scotty's monad transformer stack in order that Scotty be combined with other DSLs.
Scotty is set up by default for development mode. For production servers,
you will likely want to modify settings
and the defaultHandler
. See
the comments on each of these functions for more information.
- scottyT :: (Monad m, MonadIO n) => Port -> (forall a. m a -> n a) -> (m Response -> IO Response) -> ScottyT e m () -> n ()
- scottyAppT :: (Monad m, Monad n) => (forall a. m a -> n a) -> (m Response -> IO Response) -> ScottyT e m () -> n Application
- scottyOptsT :: (Monad m, MonadIO n) => Options -> (forall a. m a -> n a) -> (m Response -> IO Response) -> ScottyT e m () -> n ()
- data Options = Options {}
- middleware :: Monad m => Middleware -> ScottyT e m ()
- get :: (ScottyError e, MonadIO m) => RoutePattern -> ActionT e m () -> ScottyT e m ()
- post :: (ScottyError e, MonadIO m) => RoutePattern -> ActionT e m () -> ScottyT e m ()
- put :: (ScottyError e, MonadIO m) => RoutePattern -> ActionT e m () -> ScottyT e m ()
- delete :: (ScottyError e, MonadIO m) => RoutePattern -> ActionT e m () -> ScottyT e m ()
- patch :: (ScottyError e, MonadIO m) => RoutePattern -> ActionT e m () -> ScottyT e m ()
- addroute :: (ScottyError e, MonadIO m) => StdMethod -> RoutePattern -> ActionT e m () -> ScottyT e m ()
- matchAny :: (ScottyError e, MonadIO m) => RoutePattern -> ActionT e m () -> ScottyT e m ()
- notFound :: (ScottyError e, MonadIO m) => ActionT e m () -> ScottyT e m ()
- capture :: String -> RoutePattern
- regex :: String -> RoutePattern
- function :: (Request -> Maybe [Param]) -> RoutePattern
- literal :: String -> RoutePattern
- request :: (ScottyError e, Monad m) => ActionT e m Request
- header :: (ScottyError e, Monad m) => Text -> ActionT e m (Maybe Text)
- headers :: (ScottyError e, Monad m) => ActionT e m [(Text, Text)]
- body :: (ScottyError e, Monad m) => ActionT e m ByteString
- param :: (Parsable a, ScottyError e, Monad m) => Text -> ActionT e m a
- params :: (ScottyError e, Monad m) => ActionT e m [Param]
- jsonData :: (FromJSON a, ScottyError e, Monad m) => ActionT e m a
- files :: (ScottyError e, Monad m) => ActionT e m [File]
- status :: (ScottyError e, Monad m) => Status -> ActionT e m ()
- addHeader :: (ScottyError e, Monad m) => Text -> Text -> ActionT e m ()
- setHeader :: (ScottyError e, Monad m) => Text -> Text -> ActionT e m ()
- redirect :: (ScottyError e, Monad m) => Text -> ActionT e m a
- text :: (ScottyError e, Monad m) => Text -> ActionT e m ()
- html :: (ScottyError e, Monad m) => Text -> ActionT e m ()
- file :: (ScottyError e, Monad m) => FilePath -> ActionT e m ()
- json :: (ToJSON a, ScottyError e, Monad m) => a -> ActionT e m ()
- stream :: (ScottyError e, Monad m) => StreamingBody -> ActionT e m ()
- source :: (ScottyError e, Monad m) => Source IO (Flush Builder) -> ActionT e m ()
- raw :: (ScottyError e, Monad m) => ByteString -> ActionT e m ()
- raise :: (ScottyError e, Monad m) => e -> ActionT e m a
- rescue :: (ScottyError e, Monad m) => ActionT e m a -> (e -> ActionT e m a) -> ActionT e m a
- next :: (ScottyError e, Monad m) => ActionT e m a
- defaultHandler :: Monad m => (e -> ActionT e m ()) -> ScottyT e m ()
- class ScottyError e where
- stringError :: String -> e
- showError :: e -> Text
- type Param = (Text, Text)
- class Parsable a where
- parseParam :: Text -> Either Text a
- parseParamList :: Text -> Either Text [a]
- readEither :: Read a => Text -> Either Text a
- data RoutePattern
- type File = (Text, FileInfo ByteString)
- data ScottyT e m a
- data ActionT e m a
scotty-to-WAI
:: (Monad m, MonadIO n) | |
=> Port | |
-> (forall a. m a -> n a) | Run monad |
-> (m Response -> IO Response) | Run monad |
-> ScottyT e m () | |
-> n () |
Run a scotty application using the warp server. NB: scotty p === scottyT p id id
:: (Monad m, Monad n) | |
=> (forall a. m a -> n a) | Run monad |
-> (m Response -> IO Response) | Run monad |
-> ScottyT e m () | |
-> n Application |
Turn a scotty application into a WAI Application
, which can be
run with any WAI handler.
NB: scottyApp === scottyAppT id id
:: (Monad m, MonadIO n) | |
=> Options | |
-> (forall a. m a -> n a) | Run monad |
-> (m Response -> IO Response) | Run monad |
-> ScottyT e m () | |
-> n () |
Run a scotty application using the warp server, passing extra options. NB: scottyOpts opts === scottyOptsT opts id id
Options | |
|
Defining Middleware and Routes
Middleware
and routes are run in the order in which they
are defined. All middleware is run first, followed by the first
route that matches. If no route matches, a 404 response is given.
middleware :: Monad m => Middleware -> ScottyT e m ()Source
Use given middleware. Middleware is nested such that the first declared is the outermost middleware (it has first dibs on the request and last action on the response). Every middleware is run on each request.
get :: (ScottyError e, MonadIO m) => RoutePattern -> ActionT e m () -> ScottyT e m ()Source
post :: (ScottyError e, MonadIO m) => RoutePattern -> ActionT e m () -> ScottyT e m ()Source
put :: (ScottyError e, MonadIO m) => RoutePattern -> ActionT e m () -> ScottyT e m ()Source
delete :: (ScottyError e, MonadIO m) => RoutePattern -> ActionT e m () -> ScottyT e m ()Source
patch :: (ScottyError e, MonadIO m) => RoutePattern -> ActionT e m () -> ScottyT e m ()Source
addroute :: (ScottyError e, MonadIO m) => StdMethod -> RoutePattern -> ActionT e m () -> ScottyT e 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
matchAny :: (ScottyError e, MonadIO m) => RoutePattern -> ActionT e m () -> ScottyT e m ()Source
Add a route that matches regardless of the HTTP verb.
notFound :: (ScottyError e, MonadIO m) => ActionT e m () -> ScottyT e m ()Source
Specify an action to take if nothing else is found. Note: this _always_ matches, so should generally be the last route specified.
Route Patterns
capture :: String -> RoutePatternSource
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.
regex :: String -> RoutePatternSource
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
function :: (Request -> Maybe [Param]) -> RoutePatternSource
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 -> RoutePatternSource
Build a route that requires the requested path match exactly, without captures.
Accessing the Request, Captures, and Query Parameters
header :: (ScottyError e, Monad m) => Text -> ActionT e m (Maybe Text)Source
Get a request header. Header name is case-insensitive.
headers :: (ScottyError e, Monad m) => ActionT e m [(Text, Text)]Source
Get all the request headers. Header names are case-insensitive.
body :: (ScottyError e, Monad m) => ActionT e m ByteStringSource
Get the request body.
param :: (Parsable a, ScottyError e, Monad m) => Text -> ActionT e m aSource
Get a parameter. First looks in captures, then form data, then query parameters.
params :: (ScottyError e, Monad m) => ActionT e m [Param]Source
Get all parameters from capture, form and query (in that order).
jsonData :: (FromJSON a, ScottyError e, Monad m) => ActionT e m aSource
Parse the request body as a JSON object and return it. Raises an exception if parse is unsuccessful.
Modifying the Response and Redirecting
status :: (ScottyError e, Monad m) => Status -> ActionT e m ()Source
Set the HTTP response status. Default is 200.
addHeader :: (ScottyError e, Monad m) => Text -> Text -> ActionT e m ()Source
Add to the response headers. Header names are case-insensitive.
setHeader :: (ScottyError e, Monad m) => Text -> Text -> ActionT e m ()Source
Set one of the response headers. Will override any previously set value for that header. Header names are case-insensitive.
redirect :: (ScottyError e, Monad m) => Text -> ActionT e m aSource
Redirect to given URL. Like throwing an uncatchable exception. Any code after the call to redirect will not be run.
redirect "http://www.google.com"
OR
redirect "/foo/bar"
Setting Response Body
Note: only one of these should be present in any given route
definition, as they completely replace the current Response
body.
text :: (ScottyError e, Monad m) => Text -> ActionT e m ()Source
Set the body of the response to the given Text
value. Also sets "Content-Type"
header to "text/plain".
html :: (ScottyError e, Monad m) => Text -> ActionT e m ()Source
Set the body of the response to the given Text
value. Also sets "Content-Type"
header to "text/html".
file :: (ScottyError e, Monad m) => FilePath -> ActionT e m ()Source
Send a file as the response. Doesn't set the "Content-Type" header, so you probably
want to do that on your own with setHeader
.
json :: (ToJSON a, ScottyError e, Monad m) => a -> ActionT e m ()Source
Set the body of the response to the JSON encoding of the given value. Also sets "Content-Type" header to "application/json".
stream :: (ScottyError e, Monad m) => StreamingBody -> ActionT e m ()Source
Set the body of the response to a Source. Doesn't set the
"Content-Type" header, so you probably want to do that on your
own with setHeader
.
raw :: (ScottyError e, Monad m) => ByteString -> ActionT e m ()Source
Set the body of the response to the given ByteString
value. Doesn't set the
"Content-Type" header, so you probably want to do that on your
own with setHeader
.
Exceptions
raise :: (ScottyError e, Monad m) => e -> ActionT e m aSource
Throw an exception, which can be caught with rescue
. Uncaught exceptions
turn into HTTP 500 responses.
rescue :: (ScottyError e, Monad m) => ActionT e m a -> (e -> ActionT e m a) -> ActionT e m aSource
Catch an exception thrown by raise
.
raise "just kidding" `rescue` (\msg -> text msg)
next :: (ScottyError e, Monad m) => ActionT e m aSource
Abort execution of this action and continue pattern matching routes.
Like an exception, any code after next
is not executed.
As an example, these two routes overlap. The only way the second one will
ever run is if the first one calls next
.
get "/foo/:bar" $ do w :: Text <- param "bar" unless (w == "special") next text "You made a request to /foo/special" get "/foo/:baz" $ do w <- param "baz" text $ "You made a request to: " <> w
defaultHandler :: Monad m => (e -> ActionT e m ()) -> ScottyT e m ()Source
Global handler for uncaught exceptions.
Uncaught exceptions normally become 500 responses. You can use this to selectively override that behavior.
Note: IO exceptions are lifted into ScottyError
s by stringError
.
This has security implications, so you probably want to provide your
own defaultHandler in production which does not send out the error
strings as 500 responses.
class ScottyError e whereSource
In order to use a custom exception type (aside from Text
), you must
define an instance of ScottyError
for that type.
ScottyError Text | |
ScottyError e => ScottyError (ActionError e) |
Parsing Parameters
Minimum implemention: parseParam
parseParam :: Text -> Either Text aSource
Take a Text
value and parse it as a
, or fail with a message.
parseParamList :: Text -> Either Text [a]Source
Default implementation parses comma-delimited lists.
parseParamList t = mapM parseParam (T.split (== ',') t)
Parsable Bool | |
Parsable Char | Overrides default |
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 |
Parsable ByteString | |
Parsable Text | |
Parsable Text | |
Parsable a => Parsable [a] |
Types
type File = (Text, FileInfo ByteString)Source
Monad Transformers
(MonadBase b m, ScottyError e) => MonadBase b (ActionT e m) | |
(ScottyError e, MonadBaseControl b m) => MonadBaseControl b (ActionT e m) | |
ScottyError e => MonadTrans (ActionT e) | |
ScottyError e => MonadTransControl (ActionT e) | |
(ScottyError e, Monad m) => MonadError (ActionError e) (ActionT e m) | |
(Monad m, ScottyError e) => Monad (ActionT e m) | |
Functor m => Functor (ActionT e m) | |
(Monad m, Functor m) => Applicative (ActionT e m) | |
(MonadIO m, ScottyError e) => MonadIO (ActionT e m) |