| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Serv.Wai.Type
Contents
- 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)
Servers
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 Apis.
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.
Constructors
| Server | |
Fields | |
data ServerResult Source
Constructors
| 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 Servers
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 Servers
mapServer :: Monad m => (forall x. m x -> n x) -> Server m -> Server n Source
Lift an effect transformation on to a Server
Interpreting Servers
serverApplication :: Server IO -> Application Source
Converts a into a regular Wai Server IOApplication value.
serverApplication' :: Server IO -> (Context -> Response -> Response) -> Application Source
Converts a into a regular Wai Server IOApplication 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 IOApplication 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
Responses. Used by default in serverApplication'.
Contexts
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.
Constructors
| Context | |
Fields
| |
Instances
| 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
Servers 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
Methods
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
Instances
| Monad m => Contextual (StateT Context m) Source |