Safe Haskell | None |
---|---|
Language | Haskell2010 |
- run :: Port -> App () -> IO ()
- type App a = ReaderT ReqContext (ExceptT Response IO) a
- type Handler a = ReaderT ReqContext IO a
- type HeaderMap = Map (CI Text) [Text]
- route :: ToResponse r => Pattern -> Handler r -> App ()
- runHandler :: Handler r -> App r
- getPath :: ReqReader m => m Text
- getPathInfo :: ReqReader m => m [Text]
- getMethod :: ReqReader m => m Text
- getQueryString :: ReqReader m => m Text
- getQueries :: ReqReader m => m QueryMap
- getQueriesMulti :: ReqReader m => m MultiQueryMap
- getQuery :: ReqReader m => Text -> m (Maybe Text)
- getQueryMulti :: ReqReader m => Text -> m [Text]
- getHeaders :: ReqReader m => m HeaderMap
- getBody :: ReqReader m => m Text
- getCookies :: ReqReader m => m (Map Text [Text])
- getCookieMulti :: ReqReader m => Text -> m [Text]
- getCookie :: ReqReader m => Text -> m (Maybe Text)
- isSecure :: ReqReader m => m Bool
- waiRequest :: ReqReader m => m Request
- pathMatches :: ReqReader m => Pattern -> m Bool
- class ToResponse c where
- respond :: ToResponse r => r -> App ()
- respondWith :: ToResponse r => Handler r -> App ()
- newtype Json a = Json a
- addMiddleware :: App Request -> (Response -> App Response) -> App () -> App ()
- module Network.HTTP.Types.Status
Firefly Server
run :: Port -> App () -> IO () Source #
Run an http server on the given port.
-- Run app on port 3000 main :: IO () main = run 3000 app
Types
type App a = ReaderT ReqContext (ExceptT Response IO) a Source #
The main application definition monad, sequence your route handlers and/or middleware in this monad
type Handler a = ReaderT ReqContext IO a Source #
Handler is an alias for App
, currently provided for semantic reasons,
but the two may diverge in the future.
type HeaderMap = Map (CI Text) [Text] Source #
A map of case insensitive header names to all provided values for that header.
Handlers
route :: ToResponse r => Pattern -> Handler r -> App () Source #
Run a handler on a matching route.
The handler may return any type which implements ToResponse
If a route matches any actions following it in the App monad will not be run.
Route patterns support pcre regex but do not yet support url parameters.
In the following example if we call helloHarry
then the helloHandler
will run, but NOT the helloHarryHandler since a matching route
ceases further execution.
For this reason the order you define your routes in matters.
app :: App () app = do route "/" indexHandler route "/hello.*" helloHandler route "/helloHarry" helloHarryHandler
runHandler :: Handler r -> App r Source #
Runs a handler within an App WITHOUT rendering the response
Requests
getPath :: ReqReader m => m Text Source #
Calls through to rawPathInfo
; returns the full path of the current
request (without the query string)
getPathInfo :: ReqReader m => m [Text] Source #
getQueryString :: ReqReader m => m Text Source #
Calls through to rawQueryString
; returns the full path of the current
request (without the query string)
getQueries :: ReqReader m => m QueryMap Source #
Get the last set value for each query
If a query is set without a value (e.g. "/?key") it will appear in the
map with a value of ""
getQueriesMulti :: ReqReader m => m MultiQueryMap Source #
Gets all key/values from the query string
getQuery :: ReqReader m => Text -> m (Maybe Text) Source #
Get the value for a given query. A query which was passed without a value
(e.g. "/?key") will return Just ""
getQueryMulti :: ReqReader m => Text -> m [Text] Source #
Gets all values provided for a given query key
getHeaders :: ReqReader m => m HeaderMap Source #
Gets the headers of the request
getCookies :: ReqReader m => m (Map Text [Text]) Source #
Gets a map of cookies sent with the request.
getCookieMulti :: ReqReader m => Text -> m [Text] Source #
Get all values set for a specific cookie
waiRequest :: ReqReader m => m Request Source #
Exposes the underlying Request
.
pathMatches :: ReqReader m => Pattern -> m Bool Source #
Determine whether a route matches a pattern
Responses
class ToResponse c where Source #
This class represents all types which can be converted into a valid
Response
. Feel free to implement additional instances for your own
data-types.
toResponse :: c -> Response Source #
ToResponse String Source # | |
ToResponse Text Source # | |
ToResponse Value Source # | |
ToResponse Html Source # | |
ToResponse Response Source # | |
ToJSON a => ToResponse (Json a) Source # | |
ToResponse b => ToResponse (b, Status) Source # | |
ToResponse b => ToResponse (b, Status, HeaderMap) Source # | |
respond :: ToResponse r => r -> App () Source #
Respond to the client immediately. Any statements following this one in the App or Handler Monads will not be run.
respondWith :: ToResponse r => Handler r -> App () Source #
Use a given handler to respond to the request. Any handlers following this will not run.
Wrapper Types
A simple newtype wrapper you can use to wrap values, signifying they should be JSON encoded sent with the "application/json" Content-Type.
Json a |
Utilities
:: App Request | The Action to run before a |
-> (Response -> App Response) | Transform a |
-> App () | The |
-> App () |
Run actions before your handlers and/or perform actions following the response.
after
will only be run if a response is provided from some handler
Exports
Re-exported types for your convenience
module Network.HTTP.Types.Status