module Web.Welshy.Response where
import Blaze.ByteString.Builder
import Data.Aeson (ToJSON)
import qualified Data.Aeson as A
import Data.ByteString (ByteString)
import Data.Conduit
import Data.Text (Text)
import qualified Data.Text.Encoding as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TL
import Network.HTTP.Types
import Network.Wai
import Web.Welshy.Action
status :: Status -> Action ()
status s = modifyResponse $ \case
(ResponseBuilder _ h b) -> ResponseBuilder s h b
(ResponseFile _ h f fp) -> ResponseFile s h f fp
(ResponseSource _ h cs) -> ResponseSource s h cs
header :: HeaderName -> ByteString -> Action ()
header k v = modifyResponse $ \case
(ResponseBuilder s h b) -> ResponseBuilder s (update h) b
(ResponseFile s h f fp) -> ResponseFile s (update h) f fp
(ResponseSource s h cs) -> ResponseSource s (update h) cs
where update h = (k,v) : filter ((/= k) . fst) h
text :: TL.Text -> Action ()
text t = do
header hContentType "text/plain"
_builder $ fromLazyByteString $ TL.encodeUtf8 t
text' :: Text -> Action ()
text' t = do
header hContentType "text/plain"
_builder $ fromByteString $ T.encodeUtf8 t
html :: TL.Text -> Action ()
html t = do
header hContentType "text/html"
_builder $ fromLazyByteString $ TL.encodeUtf8 t
html' :: Text -> Action ()
html' t = do
header hContentType "text/html"
_builder $ fromByteString $ T.encodeUtf8 t
json :: ToJSON a => a -> Action ()
json v = do
header hContentType "application/json"
_builder $ fromLazyByteString $ A.encode v
file :: FilePath -> Action ()
file = flip _file Nothing
filePart :: FilePath -> Integer -> Integer -> Action ()
filePart f offset byteCount = _file f (Just $ FilePart offset byteCount)
_file :: FilePath -> Maybe FilePart -> Action ()
_file f part = modifyResponse $ \case
(ResponseBuilder s h _) -> ResponseFile s h f part
(ResponseFile s h _ _) -> ResponseFile s h f part
(ResponseSource s h _) -> ResponseFile s h f part
_builder :: Builder -> Action ()
_builder b = modifyResponse $ \case
(ResponseBuilder s h _) -> ResponseBuilder s h b
(ResponseFile s h _ _) -> ResponseBuilder s h b
(ResponseSource s h _) -> ResponseBuilder s h b
source :: Source (ResourceT IO) (Flush Builder) -> Action ()
source src = modifyResponse $ \case
(ResponseBuilder s h _) -> ResponseSource s h src
(ResponseFile s h _ _) -> ResponseSource s h src
(ResponseSource s h _) -> ResponseSource s h src