2017-05-25 19:12:29 +00:00
|
|
|
module HTTPure.Response
|
2017-07-14 06:28:57 +00:00
|
|
|
( ResponseM
|
|
|
|
, Response(..)
|
|
|
|
, send
|
2017-05-25 19:12:29 +00:00
|
|
|
) where
|
|
|
|
|
2017-07-10 10:17:13 +00:00
|
|
|
import Prelude (Unit, bind, discard, pure, unit)
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-07-10 10:17:13 +00:00
|
|
|
import Node.Encoding as Encoding
|
|
|
|
import Node.HTTP as HTTP
|
|
|
|
import Node.Stream as Stream
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-07-14 06:28:57 +00:00
|
|
|
import HTTPure.Body as Body
|
|
|
|
import HTTPure.Headers as Headers
|
|
|
|
import HTTPure.HTTPureM as HTTPureM
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-07-14 06:28:57 +00:00
|
|
|
-- | A response is a status, and can have headers and a body. Different response
|
|
|
|
-- | codes will allow different response components to be sent.
|
|
|
|
data Response
|
|
|
|
= OK Headers.Headers Body.Body
|
2017-05-25 19:12:29 +00:00
|
|
|
|
2017-07-14 06:28:57 +00:00
|
|
|
-- | The ResponseM type simply conveniently wraps up an HTTPure monad that
|
|
|
|
-- | returns a response. This type is the return type of all router/route
|
|
|
|
-- | methods.
|
|
|
|
type ResponseM e = HTTPureM.HTTPureM e Response
|
|
|
|
|
|
|
|
-- | Given an HTTP response and a HTTPure response, this method will return a
|
|
|
|
-- | monad encapsulating writing the HTTPure response to the HTTP response and
|
|
|
|
-- | closing the HTTP response.
|
|
|
|
send :: forall e. HTTP.Response -> Response -> HTTPureM.HTTPureM e Unit
|
|
|
|
send response (OK headers body) = do
|
|
|
|
_ <- Stream.writeString stream Encoding.UTF8 body noop
|
|
|
|
Stream.end stream noop
|
2017-07-10 10:17:13 +00:00
|
|
|
noop
|
|
|
|
where
|
2017-07-14 06:28:57 +00:00
|
|
|
stream = HTTP.responseAsStream response
|
2017-07-10 10:17:13 +00:00
|
|
|
noop = pure unit
|