Safe Haskell | None |
---|---|
Language | Haskell2010 |
- newtype Server m = Server {}
- data ServerResult
- returnServer :: Monad m => m ServerResult -> Server m
- notFound :: Monad m => Server m
- badRequest :: Monad m => Maybe String -> Server m
- methodNotAllowed :: Monad m => Set Verb -> Server m
- orElse :: Monad m => Server m -> Server m -> Server m
- mapServer :: Monad m => (forall x. m x -> n x) -> Server m -> Server n
- serverApplication :: Server IO -> Application
- serverApplication' :: Server IO -> (Context -> Response -> Response) -> Application
- serverApplication'' :: Server IO -> (Context -> ServerResult -> Response) -> Application
- defaultRoutingErrorResponse :: RoutingError -> Response
- data Context = Context {
- ctxRequest :: Request
- ctxPathZipper :: ([Text], [Text])
- ctxHeaders :: Map SomeHeaderName ByteString
- ctxHeaderAccess :: Map SomeHeaderName (Maybe Text)
- ctxQuery :: Map Text (Maybe Text)
- ctxQueryAccess :: [Text]
- ctxBody :: ByteString
- makeContext :: Request -> IO Context
- class Contextual m where
- fork :: m a -> m (a, Context)
- restore :: Context -> m ()
- getVerb :: m (Maybe Verb)
- endOfPath :: m Bool
- popSegment :: m (Maybe Text)
- popAllSegments :: m [Text]
- getHeader :: forall a n. HeaderDecode n a => Sing n -> m (Either String a)
- expectHeader :: forall n. Sing n -> Text -> m Bool
- getQuery :: QueryDecode s a => Sing s -> m (Either String a)
Server
s
The Server
type is the core type generated by this module. It's
essentially a StateT
monad storing a Context
accumulated over the
routing process of the server.
A server executing in a given monad. We construct these from Api
descriptions and corresponding Impl
descriptions for said Api
s.
Ultimately, a Server
, or at least a 'Server IO', is destined to be
transformed into a Wai Appliation
, but Server
tracks around more
information useful for interpretation and route finding.
data ServerResult Source
RoutingError RoutingError | Routing errors arise when a routing attempt fails and, depending on the error, either we should recover and backtrack or resolve the entire response with that error. |
WaiResponse Response | If the response is arising from the |
Application (Context -> Application) | If the application demands an "upgrade" or ties into another server
mechanism then routing at that location will return the (opaque)
|
Basic Server
s
returnServer :: Monad m => m ServerResult -> Server m Source
Inject a monadic result directly into a Server
badRequest :: Monad m => Maybe String -> Server m Source
A Server
which immediately fails with a BadRequest
error
methodNotAllowed :: Monad m => Set Verb -> Server m Source
A Server
which immediately fails with a MethodNotAllowed
error
Transforming Server
s
mapServer :: Monad m => (forall x. m x -> n x) -> Server m -> Server n Source
Lift an effect transformation on to a Server
Interpreting Server
s
serverApplication :: Server IO -> Application Source
Converts a
into a regular Wai Server
IO
Application
value.
serverApplication' :: Server IO -> (Context -> Response -> Response) -> Application Source
Converts a
into a regular Wai Server
IO
Application
value;
parameterized on a "response transformer" which allows a final
modification of the Wai response using information gathered from the
Context
. Useful, e.g., for writing final headers.
serverApplication'' :: Server IO -> (Context -> ServerResult -> Response) -> Application Source
Converts a
into a regular Wai Server
IO
Application
value. The
most general of the serverApplication*
functions, parameterized on
a function interpreting the Context
and ServerResult
as a Wai
Response
. As an invariant, the interpreter will never see an
Application
ServerResult
---those are handled by this function.
Utilities
defaultRoutingErrorResponse :: RoutingError -> Response Source
A straightforward way of transforming RoutingError
values to Wai
Response
s. Used by default in serverApplication'
.
Context
s
As a Server
runs it generates a Context
descrbing the routing
and decoding/encoding process so far. The Context
provides valuable
information aboue the Request
and also about how the implemtation of
the server has examined the Request
so far.
Context | |
|
Monad m => Contextual (StateT Context m) Source |
makeContext :: Request -> IO Context Source
Construct a fresh context from a Request
. Fully captures the
(normally streamed) body so that repeated accesses in the server will
all see the same body (e.g., allows for pure, strict access to the body
later).
Contextual
monads
Server
s are just monads within a server-like Context
---the
Contextual
class abstracts out several operations which we expect to
occur in such a context.
class Contextual m where Source
fork :: m a -> m (a, Context) Source
Run a computation with the current state and return it without affecting ongoing state in this thread.
restore :: Context -> m () Source
Restore a Context
.
getVerb :: m (Maybe Verb) Source
Return the HTTP verb of the current context
Return True
if there are no further path segments
popSegment :: m (Maybe Text) Source
Pops a path segment if there are any remaining
popAllSegments :: m [Text] Source
Pops all remaining path segments
getHeader :: forall a n. HeaderDecode n a => Sing n -> m (Either String a) Source
Pulls the value of a header, attempting to parse it
expectHeader :: forall n. Sing n -> Text -> m Bool Source
Asserts that we expect a header to take a given value; returns the validity of that expectation.
getQuery :: QueryDecode s a => Sing s -> m (Either String a) Source
Pulls the value of a query parameter, attempting to parse it
Monad m => Contextual (StateT Context m) Source |