It should be noted that most of the code snippets below depend on the OverloadedStrings language pragma.
- scotty :: Port -> ScottyM () -> IO ()
- scottyApp :: ScottyM () -> IO Application
- middleware :: Middleware -> ScottyM ()
- get :: Text -> ActionM () -> ScottyM ()
- post :: Text -> ActionM () -> ScottyM ()
- put :: Text -> ActionM () -> ScottyM ()
- delete :: Text -> ActionM () -> ScottyM ()
- addroute :: StdMethod -> Text -> ActionM () -> ScottyM ()
- request :: ActionM Request
- param :: Parsable a => Text -> ActionM a
- status :: Status -> ActionM ()
- header :: Text -> Text -> ActionM ()
- redirect :: Text -> ActionM ()
- text :: Text -> ActionM ()
- html :: Text -> ActionM ()
- file :: FilePath -> ActionM ()
- json :: ToJSON a => a -> ActionM ()
- raise :: Text -> ActionM a
- rescue :: ActionM a -> (Text -> ActionM a) -> ActionM a
- next :: ActionM a
- data ScottyM a
- data ActionM a
- class Parsable a
scotty-to-WAI
scottyApp :: ScottyM () -> IO ApplicationSource
Turn a scotty application into a WAI Application
, which can be
run with any WAI handler.
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 :: Middleware -> ScottyM ()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.
addroute :: StdMethod -> Text -> ActionM () -> ScottyM ()Source
Define a route with a StdMethod
, Text
value representing the path spec,
and a body (ActionM
) 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
Defining Actions
Accessing the Request, Captures, and Query Parameters
param :: Parsable a => Text -> ActionM aSource
Get a parameter. First looks in captures, then form data, then query parameters.
Modifying the Response and Redirecting
header :: Text -> Text -> ActionM ()Source
Set one of the response headers. Will override any previously set value for that header. Header names are case-insensitive.
redirect :: Text -> ActionM ()Source
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
Note: only one of these should be present in any given route
definition, as they completely replace the current Response
body.
text :: Text -> ActionM ()Source
Set the body of the response to the given Text
value. Also sets "Content-Type"
header to "text/plain".
html :: Text -> ActionM ()Source
Set the body of the response to the given Text
value. Also sets "Content-Type"
header to "text/html".
file :: FilePath -> ActionM ()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 header
.
json :: ToJSON a => a -> ActionM ()Source
Set the body of the response to the JSON encoding of the given value. Also sets "Content-Type" header to "application/json".
Exceptions
raise :: Text -> ActionM aSource
Throw an exception, which can be caught with rescue
. Uncaught exceptions
turn into HTTP 500 responses.
rescue :: ActionM a -> (Text -> ActionM a) -> ActionM aSource
Catch an exception thrown by raise
.
raise "just kidding" `rescue` (\msg -> text msg)
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/:number" $ do n <- param "number" unless (all isDigit n) $ next text "a number" get "/foo/:bar" $ do bar <- param "bar" text "not a number"
Types
Monad ActionM | |
Functor ActionM | |
MonadIO ActionM | |
MonadError ActionError ActionM | |
MonadState Response ActionM | |
MonadReader (Request, [Param]) ActionM |