Use Evaluate instead of Bool

This commit is contained in:
Sasa Bogicevic 2018-07-09 23:35:38 +02:00
parent 465e0b8f10
commit a786dc421f
No known key found for this signature in database
GPG key ID: FB17B988AAEEB39E
4 changed files with 13 additions and 6 deletions

View file

@ -132,7 +132,7 @@ serve p = serveWithContext p EmptyContext
serveWithContext :: (HasServer api context)
=> Proxy api -> Context context -> Server api -> Application
serveWithContext p context server =
toApplication True (runRouter (route p context (emptyDelayed (Route server))))
toApplication Force (runRouter (route p context (emptyDelayed (Route server))))
-- | Hoist server implementation.
--

View file

@ -46,3 +46,10 @@ instance MonadBaseControl IO Handler where
runHandler :: Handler a -> IO (Either ServantErr a)
runHandler = runExceptT . runHandler'
-- determins if response should be reduced to NF
data Evaluate =
Force
| Lazy
deriving (Show)

View file

@ -95,16 +95,16 @@ instance MonadTransControl RouteResultT where
instance MonadThrow m => MonadThrow (RouteResultT m) where
throwM = lift . throwM
toApplication :: Bool -> RoutingApplication -> Application
toApplication :: Evaluate -> RoutingApplication -> Application
toApplication fullyEvaluate ra request respond =
ra request (maybeEval routingRespond)
where
maybeEval :: (RouteResult Response -> IO ResponseReceived)
-> RouteResult Response -> IO ResponseReceived
maybeEval resp =
if fullyEvaluate
then force resp
else resp
case fullyEvaluate of
Force -> force resp
Lazy -> resp
routingRespond :: RouteResult Response -> IO ResponseReceived
routingRespond (Fail err) = respond $ responseServantErr err
routingRespond (FailFatal err) = respond $ responseServantErr err

View file

@ -31,7 +31,7 @@ spec = describe "Servant.Server.Internal.Router" $ do
routerSpec :: Spec
routerSpec = do
let app' :: Application
app' = toApplication $ runRouter router'
app' = toApplication Force $ runRouter router'
router', router :: Router ()
router' = tweakResponse (fmap twk) router