License | Apache License 2.0 |
---|---|
Maintainer | aditya.mmy@gmail.com |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Synopsis
- data WebbyM appEnv a
- data RoutePattern
- type Route env = (RoutePattern, WebbyM env ())
- mkRoute :: Method -> Text -> WebbyM appEnv () -> (RoutePattern, WebbyM appEnv ())
- post :: Text -> WebbyM appEnv () -> (RoutePattern, WebbyM appEnv ())
- get :: Text -> WebbyM appEnv () -> (RoutePattern, WebbyM appEnv ())
- put :: Text -> WebbyM appEnv () -> (RoutePattern, WebbyM appEnv ())
- delete :: Text -> WebbyM appEnv () -> (RoutePattern, WebbyM appEnv ())
- type Captures = HashMap Text Text
- captures :: WebbyM appEnv Captures
- getCapture :: FromHttpApiData a => Text -> WebbyM appEnv a
- flag :: Text -> WebbyM appEnv Bool
- header :: HeaderName -> WebbyM appEnv (Maybe Text)
- headers :: WebbyM appEnv [Header]
- jsonData :: FromJSON a => WebbyM appEnv a
- param :: FromHttpApiData a => Text -> WebbyM appEnv (Maybe a)
- param_ :: FromHttpApiData a => Text -> WebbyM appEnv a
- params :: WebbyM appEnv [(Text, Text)]
- request :: WebbyM appEnv Request
- requestBodyLBS :: WebbyM appEnv LByteString
- requestBodyLength :: WebbyM appEnv (Maybe Int64)
- getRequestBodyChunkAction :: WebbyM appEnv (WebbyM appEnv ByteString)
- setStatus :: Status -> WebbyM appEnv ()
- addHeader :: Header -> WebbyM appEnv ()
- setHeader :: Header -> WebbyM appEnv ()
- blob :: ByteString -> WebbyM appEnv ()
- json :: ToJSON b => b -> WebbyM appEnv ()
- text :: Text -> WebbyM appEnv ()
- stream :: StreamingBody -> WebbyM appEnv ()
- image :: ByteString -> MimeType -> WebbyM appEnv ()
- raw :: ByteString -> WebbyM appEnv ()
- mkWebbyApp :: env -> WebbyServerConfig env -> IO Application
- type Application = Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
- data WebbyServerConfig env
- defaultWebbyServerConfig :: WebbyServerConfig env
- setRoutes :: [Route env] -> WebbyServerConfig env -> WebbyServerConfig env
- setExceptionHandler :: Exception e => (e -> WebbyM env ()) -> WebbyServerConfig env -> WebbyServerConfig env
- finish :: WebbyM appEnv a
- data WebbyError
Documentation
The main monad transformer stack used in the web-framework.
The type of a handler for a request is WebbyM appEnv ()
. The appEnv
parameter is used by the web application to store an (read-only) environment.
For e.g. it can be used to store a database connection pool.
Instances
MonadReader appEnv (WebbyM appEnv) Source # | |
MonadIO (WebbyM appEnv) Source # | |
Defined in Webby.Types | |
Applicative (WebbyM appEnv) Source # | |
Defined in Webby.Types pure :: a -> WebbyM appEnv a # (<*>) :: WebbyM appEnv (a -> b) -> WebbyM appEnv a -> WebbyM appEnv b # liftA2 :: (a -> b -> c) -> WebbyM appEnv a -> WebbyM appEnv b -> WebbyM appEnv c # (*>) :: WebbyM appEnv a -> WebbyM appEnv b -> WebbyM appEnv b # (<*) :: WebbyM appEnv a -> WebbyM appEnv b -> WebbyM appEnv a # | |
Functor (WebbyM appEnv) Source # | |
Monad (WebbyM appEnv) Source # | |
MonadUnliftIO (WebbyM appEnv) Source # | |
Defined in Webby.Types |
Routing and handler functions
data RoutePattern Source #
A route pattern represents logic to match a request to a handler.
Instances
Show RoutePattern Source # | |
Defined in Webby.Types showsPrec :: Int -> RoutePattern -> ShowS # show :: RoutePattern -> String # showList :: [RoutePattern] -> ShowS # | |
Eq RoutePattern Source # | |
Defined in Webby.Types (==) :: RoutePattern -> RoutePattern -> Bool # (/=) :: RoutePattern -> RoutePattern -> Bool # |
type Route env = (RoutePattern, WebbyM env ()) Source #
A route is a pair of a route pattern and a handler.
mkRoute :: Method -> Text -> WebbyM appEnv () -> (RoutePattern, WebbyM appEnv ()) Source #
Create a route for a user-provided HTTP request method, pattern and handler function.
post :: Text -> WebbyM appEnv () -> (RoutePattern, WebbyM appEnv ()) Source #
Create a route for a POST
request method, given the path pattern
and handler.
get :: Text -> WebbyM appEnv () -> (RoutePattern, WebbyM appEnv ()) Source #
Create a route for a GET
request method, given the path pattern
and handler.
put :: Text -> WebbyM appEnv () -> (RoutePattern, WebbyM appEnv ()) Source #
Create a route for a PUT
request method, given the path pattern
and handler.
delete :: Text -> WebbyM appEnv () -> (RoutePattern, WebbyM appEnv ()) Source #
Create a route for a DELETE
request method, given path pattern and handler.
Captures
getCapture :: FromHttpApiData a => Text -> WebbyM appEnv a Source #
Retrieve a particular capture
Request parsing
jsonData :: FromJSON a => WebbyM appEnv a Source #
Parse the request body as a JSON object and return it. Raises
WebbyJSONParseError
exception if parsing is unsuccessful.
param :: FromHttpApiData a => Text -> WebbyM appEnv (Maybe a) Source #
Gets the given query param's value
param_ :: FromHttpApiData a => Text -> WebbyM appEnv a Source #
Similar to param
except that it returns the handler with a '400
BadRequest' if the query param is missing.
params :: WebbyM appEnv [(Text, Text)] Source #
Get all request query params as a list of key-value pairs
requestBodyLBS :: WebbyM appEnv LByteString Source #
Return the raw request body as a lazy bytestring
getRequestBodyChunkAction :: WebbyM appEnv (WebbyM appEnv ByteString) Source #
Returns an action that returns successive chunks of the rquest body. It returns an empty bytestring after the request body is consumed.
Response modification
blob :: ByteString -> WebbyM appEnv () Source #
Send a binary stream in the response body. Also
sets Content-Type
header to application/octet-stream
json :: ToJSON b => b -> WebbyM appEnv () Source #
Set the body of the response to the JSON encoding of the given value. Also
sets Content-Type
header to application/json; charset=utf-8
text :: Text -> WebbyM appEnv () Source #
Send plain-text in the response body. Also
sets Content-Type
header to text/plain; charset=utf-8
stream :: StreamingBody -> WebbyM appEnv () Source #
Set the body of the response to a StreamingBody. Doesn't set the
Content-Type
header, so you probably want to do that on your own with
setHeader
.
image :: ByteString -> MimeType -> WebbyM appEnv () Source #
Send an image in the response body. Also
sets Content-Type
header to @mimeType
e.g. image/svg+xml
raw :: ByteString -> WebbyM appEnv () Source #
Send a binary stream in the response body. Doesn't
set Content-Type
header
Application
mkWebbyApp :: env -> WebbyServerConfig env -> IO Application Source #
Use this function to create a WAI application. It takes a user/application
defined appEnv
data type and a list of routes. Routes are matched in the
given order. If none of the requests match a request, a default 404 response
is returned.
type Application = Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived #
The WAI application.
Note that, since WAI 3.0, this type is structured in continuation passing
style to allow for proper safe resource handling. This was handled in the
past via other means (e.g., ResourceT
). As a demonstration:
app :: Application app req respond = bracket_ (putStrLn "Allocating scarce resource") (putStrLn "Cleaning up") (respond $ responseLBS status200 [] "Hello World")
Webby server configuration
data WebbyServerConfig env Source #
Holds web server configuration like API routes, handlers and an optional exception handler
defaultWebbyServerConfig :: WebbyServerConfig env Source #
Default WebbyServerConfig
typically used in conjunction with setRoutes
and setExceptionHandler
setRoutes :: [Route env] -> WebbyServerConfig env -> WebbyServerConfig env Source #
Sets API routes and their handlers of a WebbyServerConfig
setExceptionHandler :: Exception e => (e -> WebbyM env ()) -> WebbyServerConfig env -> WebbyServerConfig env Source #
Sets the exception handler of a WebbyServerConfig
Handler flow control
Exceptions thrown
data WebbyError Source #
Various kinds of errors thrown by this library - these can be caught by handler code.
WebbyJSONParseError Text | |
WebbyParamParseError | |
| |
WebbyMissingCapture Text |
Instances
Exception WebbyError Source # | |
Defined in Webby.Types toException :: WebbyError -> SomeException # fromException :: SomeException -> Maybe WebbyError # displayException :: WebbyError -> String # | |
Show WebbyError Source # | |
Defined in Webby.Types showsPrec :: Int -> WebbyError -> ShowS # show :: WebbyError -> String # showList :: [WebbyError] -> ShowS # |