Safe Haskell | None |
---|
- welshy :: Port -> Welshy () -> IO ()
- welshyApp :: Welshy () -> Application
- data Welshy a
- middleware :: Middleware -> Welshy ()
- route :: StdMethod -> RoutePattern -> Action () -> Welshy ()
- type RoutePattern = Text
- get :: RoutePattern -> Action () -> Welshy ()
- post :: RoutePattern -> Action () -> Welshy ()
- put :: RoutePattern -> Action () -> Welshy ()
- patch :: RoutePattern -> Action () -> Welshy ()
- delete :: RoutePattern -> Action () -> Welshy ()
- head :: RoutePattern -> Action () -> Welshy ()
- options :: RoutePattern -> Action () -> Welshy ()
- data Action a
- halt :: Action () -> Action a
- pass :: Action a
- catchIO :: Exception e => IO a -> (e -> Action a) -> Action a
- request :: Action Request
- body :: Action ByteString
- capture :: FromText a => Text -> Action a
- captures :: Action [Param]
- queryParam :: FromText a => Text -> Action a
- maybeQueryParam :: FromText a => Text -> Action (Maybe a)
- queryParams :: Action [Param]
- jsonParam :: FromJSON a => Text -> Action a
- maybeJsonParam :: FromJSON a => Text -> Action (Maybe a)
- jsonParams :: Action Object
- jsonData :: FromJSON a => Action a
- bearerAuth :: FromText a => Action a
- status :: Status -> Action ()
- header :: HeaderName -> ByteString -> Action ()
- text :: Text -> Action ()
- text' :: Text -> Action ()
- html :: Text -> Action ()
- html' :: Text -> Action ()
- json :: ToJSON a => a -> Action ()
- file :: FilePath -> Action ()
- filePart :: FilePath -> Integer -> Integer -> Action ()
- source :: Source (ResourceT IO) (Flush Builder) -> Action ()
- type Param = (Text, Text)
- class FromText a where
- maybeFromText :: FromText a => Text -> Maybe a
Documentation
welshyApp :: Welshy () -> ApplicationSource
Turns a Welshy app into a WAI Application
.
Middleware & Routing
We use this monad to compose WAI Middleware
, using the middleware
and route
functions.
middleware :: Middleware -> Welshy ()Source
Insert middleware into the app. Note that unlike in Scotty, each middleware is run at the point of insertion.
route :: StdMethod -> RoutePattern -> Action () -> Welshy ()Source
Define a route for an HTTP method and URL pattern that runs the given action. Routes are matched in the order they are defined. If no route matches, a 404 response is returned.
type RoutePattern = TextSource
Sinatra-style route pattern. Named parameters are prepended with
a colon (e.g. "/users/:id"
) and can be accessed with capture
.
get :: RoutePattern -> Action () -> Welshy ()Source
post :: RoutePattern -> Action () -> Welshy ()Source
put :: RoutePattern -> Action () -> Welshy ()Source
patch :: RoutePattern -> Action () -> Welshy ()Source
delete :: RoutePattern -> Action () -> Welshy ()Source
head :: RoutePattern -> Action () -> Welshy ()Source
options :: RoutePattern -> Action () -> Welshy ()Source
Actions
halt :: Action () -> Action aSource
Stop running the current action and continue with another one. The other action will live in the same request environment and can access the same route captures, but it will start with a fresh default response.
This is incredibly useful for error handling. For example:
patch "/users/:uid" $ do uid <- capture "uid" user <- getUserFromDB uid `catchIO` (\_ -> halt $ status notFound404) ...
Request
body :: Action ByteStringSource
Get the request body.
capture :: FromText a => Text -> Action aSource
Get a parameter captured by the route pattern.
- If the parameter does not exist, fails with an error.
- If the parameter was found but could not be parsed,
pass
is called. This means routes are typed to a degree.
queryParam :: FromText a => Text -> Action aSource
Get a query parameter.
- If the parameter does not exist or could not be parsed,
the action halts with HTTP status
400 Bad Request
.
maybeQueryParam :: FromText a => Text -> Action (Maybe a)Source
Like queryParam
, but returns Nothing
if the parameter wasn't found.
- If the parameter could not be parsed,
the action halts with HTTP status
400 BadRequest
queryParams :: Action [Param]Source
Get all query parameters.
jsonParam :: FromJSON a => Text -> Action aSource
Get a JSON parameter.
- If the request body is not a JSON dictionary,
or if the parameter does not exist or could not be parsed,
the action halts with HTTP status
400 Bad Request
.
maybeJsonParam :: FromJSON a => Text -> Action (Maybe a)Source
jsonParams :: Action ObjectSource
Get all JSON parameters.
- If the request body is not a JSON dictionary,
the action halts with HTTP status
400 Bad Request
.
jsonData :: FromJSON a => Action aSource
Parse the request body as a JSON object.
- If the body could not be parsed,
the action halts with HTTP status
400 Bad Request
.
bearerAuth :: FromText a => Action aSource
Get the bearer token from an authorization header using the Bearer
authentication scheme (RFC 6750).
If the request does not have a (syntactically) valid authorization
header for the Bearer scheme, the action halts with HTTP status
401 Unauthorized
.
Response
header :: HeaderName -> ByteString -> Action ()Source
Add or replace one of the response headers.
text :: Text -> Action ()Source
Set the response body to the given lazy Text
and the content-type to text/plain
.
html :: Text -> Action ()Source
Set the response body to the given lazy Text
and the content-type to text/html
.
json :: ToJSON a => a -> Action ()Source
Set the response body to the JSON encoding of the given value
and the content-type to application/json
.
filePart :: FilePath -> Integer -> Integer -> Action ()Source
filePart
f offset byteCount
sends byteCount
bytes of the file f
,
beginning at offset
, as the response.
source :: Source (ResourceT IO) (Flush Builder) -> Action ()Source
Set the response body to the given Source
.
Parameter Parsing
A type that can be converted from a strict Text
value.
Used for parsing route captures, query parameters, header values, etc.
Minimal complete definition: fromText
.
maybeFromText :: FromText a => Text -> Maybe aSource
maybeFromText = either (const Nothing) Just . fromText