Skip to content

Commit 547adab

Browse files
author
Sasa Bogicevic
committed
Add NFData instance
add deepseq dep Hardcode bool param for now
1 parent ff4e2af commit 547adab

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

servant-server/servant-server.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ library
8383
, base-compat >= 0.9.3 && < 0.11
8484
, attoparsec >= 0.13.2.0 && < 0.14
8585
, base64-bytestring >= 1.0.0.1 && < 1.1
86+
, deepseq >= 1.4.3.0 && < 1.5
8687
, exceptions >= 0.8.3 && < 0.11
8788
, http-api-data >= 0.3.7.1 && < 0.4
8889
, http-media >= 0.7.1.1 && < 0.8

servant-server/src/Servant/Server.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,14 @@ import Servant.Server.Internal
125125
serve :: (HasServer api '[]) => Proxy api -> Server api -> Application
126126
serve p = serveWithContext p EmptyContext
127127

128+
type FullyEvaluateResponse = Bool
129+
128130
serveWithContext :: (HasServer api context)
129131
=> Proxy api -> Context context -> Server api -> Application
130132
serveWithContext p context server =
131-
toApplication (runRouter (route p context (emptyDelayed (Route server))))
133+
toApplication
134+
False
135+
(runRouter (route p context (emptyDelayed (Route server))))
132136

133137
-- | Hoist server implementation.
134138
--

servant-server/src/Servant/Server/Internal/RoutingApplication.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
{-# LANGUAGE UndecidableInstances #-}
1111
module Servant.Server.Internal.RoutingApplication where
1212

13+
import Control.DeepSeq (force)
1314
import Control.Monad (ap, liftM)
1415
import Control.Monad.Base (MonadBase (..))
1516
import Control.Monad.Catch (MonadThrow (..))
@@ -84,9 +85,12 @@ instance MonadTransControl RouteResultT where
8485
instance MonadThrow m => MonadThrow (RouteResultT m) where
8586
throwM = lift . throwM
8687

87-
toApplication :: RoutingApplication -> Application
88-
toApplication ra request respond = ra request routingRespond
88+
toApplication :: Bool -> RoutingApplication -> Application
89+
toApplication fullyEvaluate ra request respond = ra request routingRespond
8990
where
91+
maybeEval :: (RouteResult Response -> IO ResponseReceived)
92+
-> RouteResult Response -> IO ResponseReceived
93+
maybeEval resp = if fullyEvaluate then force resp else resp
9094
routingRespond :: RouteResult Response -> IO ResponseReceived
9195
routingRespond (Fail err) = respond $ responseServantErr err
9296
routingRespond (FailFatal err) = respond $ responseServantErr err

servant-server/src/Servant/Server/Internal/ServantErr.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
{-# LANGUAGE OverloadedStrings #-}
2-
{-# LANGUAGE RecordWildCards #-}
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE RecordWildCards #-}
33
{-# LANGUAGE DeriveDataTypeable #-}
4+
{-# LANGUAGE DeriveGeneric #-}
5+
{-# LANGUAGE DeriveAnyClass #-}
46
module Servant.Server.Internal.ServantErr where
57

8+
import Control.DeepSeq (NFData)
69
import Control.Exception (Exception)
710
import qualified Data.ByteString.Char8 as BS
811
import qualified Data.ByteString.Lazy as LBS
912
import Data.Typeable (Typeable)
1013
import qualified Network.HTTP.Types as HTTP
1114
import Network.Wai (Response, responseLBS)
15+
import GHC.Generics (Generic)
1216

1317
data ServantErr = ServantErr { errHTTPCode :: Int
1418
, errReasonPhrase :: String
1519
, errBody :: LBS.ByteString
1620
, errHeaders :: [HTTP.Header]
17-
} deriving (Show, Eq, Read, Typeable)
21+
} deriving (Show, Eq, Read, Typeable, Generic, NFData)
1822

1923
instance Exception ServantErr
2024

0 commit comments

Comments
 (0)