firefly-0.2.1.0: A simple HTTP server framework

Safe HaskellNone
LanguageHaskell2010

Web.Firefly

Contents

Synopsis

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 #

Calls through to pathInfo. Returns the path's individual / separated chunks. '

getMethod :: ReqReader m => m Text Source #

Gets the HTTP method of the current request

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

getBody :: ReqReader m => m Text Source #

Gets full body 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

getCookie :: ReqReader m => Text -> m (Maybe Text) Source #

Get the value for a cookie if it is set

isSecure :: ReqReader m => m Bool Source #

Calls through to isSecure

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.

Minimal complete definition

toResponse

Methods

toResponse :: c -> Response 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

newtype Json a Source #

A simple newtype wrapper you can use to wrap values, signifying they should be JSON encoded sent with the "application/json" Content-Type.

Constructors

Json a 

Instances

Show a => Show (Json a) Source # 

Methods

showsPrec :: Int -> Json a -> ShowS #

show :: Json a -> String #

showList :: [Json a] -> ShowS #

ToJSON a => ToResponse (Json a) Source # 

Utilities

addMiddleware Source #

Arguments

:: App Request

The Action to run before a Request is processed. The modified request which is returned will be passed to the app.

-> (Response -> App Response)

Transform a Response before it's sent

-> App ()

The App to wrap with middleware

-> 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