Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Internal types and functions
Synopsis
- newtype Server m = Server {}
- data Req = Req {}
- data Resp = Resp {}
- data RespBody
- type QueryMap = Map ByteString ByteString
- class ToText a where
- data Error a = Error {}
- toConst :: Functor m => m Resp -> Server m
- toMethod :: Monad m => Method -> m Resp -> Server m
- toWithBody :: MonadIO m => (ByteString -> Server m) -> Server m
- toWithCapture :: Monad m => (Text -> Server m) -> Server m
- toWithPath :: Monad m => Text -> Server m -> Server m
- toWithHeader :: (Monad m, FromHttpApiData a) => HeaderName -> (Maybe a -> Server m) -> Server m
- toWithFormData :: (FromForm a, MonadIO m) => (a -> Server m) -> Server m
- toWithPathInfo :: ([Text] -> Server m) -> Server m
- text :: ToText a => a -> Resp
- json :: ToJSON resp => resp -> Resp
- html :: ToMarkup a => a -> Resp
- raw :: ByteString -> Resp
- ok :: ResponseHeaders -> RespBody -> Resp
- badRequest :: Text -> Resp
- setContent :: ByteString -> ResponseHeaders
- data ServerConfig = ServerConfig {}
- type Kilobytes = Int
- toApplication :: ServerConfig -> Server IO -> Application
- setRespStatus :: Status -> Resp -> Resp
- handleError :: (Exception a, MonadCatch m) => (a -> Server m) -> Server m -> Server m
- toResponse :: Resp -> Response
- fromRequest :: Maybe Kilobytes -> Request -> IO Req
- pathHead :: Req -> Maybe (Text, Req)
types
Server type. It is a function fron request to response. Some servers does not return valid value. We use it to find right path.
Example:
server :: Server IO server = "api" /. "v1" /. mconcat [ "foo" /. (\(Query @"name" arg) -> Get @Json (handleFoo arg) , "bar" /. Post @Json handleBar ] handleFoo :: Int -> IO Text handleBar :: IO Text
Note that server is monoid and it can be constructed with Monoid functions and
path constructor (/.)
. To pass inputs for handler we can use special newtype wrappers:
Query
- for required query parametersOptional
- for optional query parametersCapture
- for parsing elements of URIBody
- fot JSON-body inputRawBody
- for raw ByteString inputHeader
- for headers
To distinguish by HTTP-method we use corresponding constructors: Get, Post, Put, etc. Let's discuss the structure of the constructor. Let's take Get for example:
newtype Get ty m a = Get (m a)
Let's look at the arguments of he type
ty
- type of the response. it can be: Text, Html, Json, ByteStringm
- underlying server monada
- result type. It should be convertible to the type of the response.
also result can be wrapped to special data types to modify Http-response. we have wrappers:
SetStatus
- to set statusAddHeaders
- to append headersEither (Error err)
- to response with errors
Http request
Http response
Instances
IsString Resp Source # | |
Defined in Mig.Internal.Types fromString :: String -> Resp # |
Http response body
type QueryMap = Map ByteString ByteString Source #
Map of query parameters for fast-access
Values convertible to lazy text
Errors
Instances
(Typeable a, Show a) => Exception (Error a) Source # | |
Defined in Mig.Internal.Types toException :: Error a -> SomeException # fromException :: SomeException -> Maybe (Error a) # displayException :: Error a -> String # | |
Show a => Show (Error a) Source # | |
(Show a, Typeable a) => HasServer (ReaderT env (ExceptT (Error a) IO)) Source # | |
(ToJSON err, ToHtmlResp a) => ToHtmlResp (Either (Error err) a) Source # | |
(ToJSON err, ToJsonResp a) => ToJsonResp (Either (Error err) a) Source # | |
(ToText err, ToTextResp a) => ToTextResp (Either (Error err) a) Source # | |
type ServerResult (ReaderT env (ExceptT (Error a) IO)) Source # | |
constructors
toWithBody :: MonadIO m => (ByteString -> Server m) -> Server m Source #
Reads full body as lazy bytestring
toWithHeader :: (Monad m, FromHttpApiData a) => HeaderName -> (Maybe a -> Server m) -> Server m Source #
Read info from header
toWithFormData :: (FromForm a, MonadIO m) => (a -> Server m) -> Server m Source #
Reads URL-encoded form data
responses
raw :: ByteString -> Resp Source #
Raw bytestring response constructor
badRequest :: Text -> Resp Source #
Bad request response
setContent :: ByteString -> ResponseHeaders Source #
Headers to set content type
WAI
toApplication :: ServerConfig -> Server IO -> Application Source #
Convert server to WAI-application
utils
handleError :: (Exception a, MonadCatch m) => (a -> Server m) -> Server m -> Server m Source #
Handle errors
toResponse :: Resp -> Response Source #
Convert response to low-level WAI-response