This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CBR-179] Move middlewares to dedicated module
- Loading branch information
Showing
5 changed files
with
58 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{- | A collection of middlewares used by this edge node. | ||
@Middleware@ is a component that sits between the server and application. | ||
It can do such tasks as GZIP encoding or response caching. | ||
-} | ||
|
||
module Cardano.Wallet.Server.Middlewares | ||
( withMiddlewares | ||
, throttleMiddleware | ||
) where | ||
|
||
import Universum | ||
|
||
import Data.Aeson (encode) | ||
import Network.Wai (Application, Middleware, responseLBS) | ||
import qualified Network.Wai.Middleware.Throttle as Throttle | ||
|
||
import Cardano.Wallet.API.V1.Headers (applicationJson) | ||
import qualified Cardano.Wallet.API.V1.Types as V1 | ||
|
||
import Pos.Launcher.Configuration (ThrottleSettings (..)) | ||
|
||
-- | "Attaches" the middlewares to this 'Application'. | ||
withMiddlewares :: [Middleware] -> Application -> Application | ||
withMiddlewares = flip $ foldr ($) | ||
|
||
-- | A @Middleware@ to throttle requests. | ||
throttleMiddleware :: Maybe ThrottleSettings -> Middleware | ||
throttleMiddleware Nothing app = app | ||
throttleMiddleware (Just ts) app = \req respond -> do | ||
throttler <- Throttle.initThrottler | ||
Throttle.throttle throttleSettings throttler app req respond | ||
where | ||
throttleSettings = Throttle.defaultThrottleSettings | ||
{ Throttle.onThrottled = \microsTilRetry -> | ||
let | ||
err = V1.RequestThrottled microsTilRetry | ||
in | ||
responseLBS (V1.toHttpErrorStatus err) [applicationJson] (encode err) | ||
, Throttle.throttleRate = fromIntegral $ tsRate ts | ||
, Throttle.throttlePeriod = fromIntegral $ tsPeriod ts | ||
, Throttle.throttleBurst = fromIntegral $ tsBurst ts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters