Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit dcce5c1

Browse files
akegaljKtorZ
authored andcommitted
[CBR-179] Move middlewares to dedicated module
1 parent c536af4 commit dcce5c1

File tree

5 files changed

+58
-51
lines changed

5 files changed

+58
-51
lines changed

wallet-new/cardano-sl-wallet-new.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ library
134134
Cardano.Wallet.Orphans.Bi
135135
Cardano.Wallet.Server
136136
Cardano.Wallet.Server.CLI
137+
Cardano.Wallet.Server.Middlewares
137138
Cardano.Wallet.Server.Plugins
138139
Cardano.Wallet.Server.Plugins.AcidState
139140
Cardano.Wallet.Server.LegacyPlugins

wallet-new/server/Main.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import Cardano.Wallet.Server.CLI (ChooseWalletBackend (..),
5050
getWalletNodeOptions, walletDbPath, walletFlushDb,
5151
walletRebuildDb)
5252
import qualified Cardano.Wallet.Server.LegacyPlugins as LegacyPlugins
53+
import Cardano.Wallet.Server.Middlewares (throttleMiddleware)
5354
import qualified Cardano.Wallet.Server.Plugins as Plugins
5455
import Cardano.Wallet.WalletLayer (PassiveWalletLayer)
5556
import qualified Cardano.Wallet.WalletLayer.Kernel as WalletLayer.Kernel
@@ -107,7 +108,7 @@ actionWithLegacyWallet coreConfig walletConfig txpConfig sscParams nodeParams nt
107108
plugins ntpStatus =
108109
mconcat [ LegacyPlugins.conversation wArgs
109110
, LegacyPlugins.legacyWalletBackend coreConfig txpConfig wArgs ntpStatus
110-
[ LegacyPlugins.throttleMiddleware (ccThrottle walletConfig)
111+
[ throttleMiddleware (ccThrottle walletConfig)
111112
]
112113
, LegacyPlugins.walletDocumentation wArgs
113114
, LegacyPlugins.acidCleanupWorker wArgs
@@ -175,7 +176,7 @@ actionWithWallet coreConfig walletConfig txpConfig sscParams nodeParams ntpConfi
175176
-- The actual wallet backend server.
176177
[ Plugins.apiServer pm params w
177178
-- Throttle requests.
178-
[ Plugins.throttleMiddleware (ccThrottle walletConfig)
179+
[ throttleMiddleware (ccThrottle walletConfig)
179180
]
180181

181182
-- The corresponding wallet documention, served as a different

wallet-new/src/Cardano/Wallet/Server/LegacyPlugins.hs

+3-31
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module Cardano.Wallet.Server.LegacyPlugins (
1313
, walletDocumentation
1414
, resubmitterPlugin
1515
, notifierPlugin
16-
, throttleMiddleware
1716
) where
1817

1918
import Universum
@@ -24,6 +23,7 @@ import qualified Cardano.Wallet.API.V1.Types as V1
2423
import qualified Cardano.Wallet.LegacyServer as LegacyServer
2524
import Cardano.Wallet.Server.CLI (WalletBackendParams (..),
2625
isDebugMode, walletAcidInterval, walletDbOptions)
26+
import Cardano.Wallet.Server.Middlewares (withMiddlewares)
2727
import qualified Pos.Wallet.Web.Error.Types as V0
2828

2929
import Control.Exception (fromException)
@@ -33,7 +33,6 @@ import Network.HTTP.Types.Status (badRequest400)
3333
import Network.Wai (Application, Middleware, Response, responseLBS)
3434
import Network.Wai.Handler.Warp (defaultSettings,
3535
setOnExceptionResponse)
36-
import qualified Network.Wai.Middleware.Throttle as Throttle
3736
import Ntp.Client (NtpStatus)
3837
import Pos.Chain.Txp (TxpConfiguration)
3938
import Pos.Infra.Diffusion.Types (Diffusion (..))
@@ -54,8 +53,7 @@ import Cardano.NodeIPC (startNodeJsIPC)
5453
import Pos.Configuration (walletProductionApi,
5554
walletTxCreationDisabled)
5655
import Pos.Infra.Shutdown.Class (HasShutdownContext (shutdownContext))
57-
import Pos.Launcher.Configuration (HasConfigurations,
58-
ThrottleSettings (..))
56+
import Pos.Launcher.Configuration (HasConfigurations)
5957
import Pos.Util.CompileInfo (HasCompileInfo)
6058
import Pos.Wallet.Web.Mode (WalletWebMode)
6159
import Pos.Wallet.Web.Server.Launcher (walletDocumentationImpl,
@@ -103,11 +101,7 @@ walletDocumentation WalletBackendParams {..} = pure $ \_ ->
103101
application :: WalletWebMode Application
104102
application = do
105103
let app = Servant.serve API.walletDocAPI LegacyServer.walletDocServer
106-
return $
107-
withMiddlewares
108-
[ throttleMiddleware Nothing
109-
]
110-
app
104+
return $ withMiddlewares [] app
111105
tls =
112106
if isDebugMode walletRunMode then Nothing else walletTLSParams
113107

@@ -215,25 +209,3 @@ syncWalletWorker :: Core.Config -> Plugin WalletWebMode
215209
syncWalletWorker coreConfig = pure $ const $
216210
modifyLoggerName (const "syncWalletWorker") $
217211
(view (lensOf @SyncQueue) >>= processSyncRequest coreConfig)
218-
219-
-- | "Attaches" the middlewares to this 'Application'.
220-
withMiddlewares :: [Middleware] -> Application -> Application
221-
withMiddlewares = flip $ foldr ($)
222-
223-
-- | A @Middleware@ to throttle requests.
224-
throttleMiddleware :: Maybe ThrottleSettings -> Middleware
225-
throttleMiddleware Nothing app = app
226-
throttleMiddleware (Just ts) app = \req respond -> do
227-
throttler <- Throttle.initThrottler
228-
Throttle.throttle throttleSettings throttler app req respond
229-
where
230-
throttleSettings = Throttle.defaultThrottleSettings
231-
{ Throttle.onThrottled = \microsTilRetry ->
232-
let
233-
err = V1.RequestThrottled microsTilRetry
234-
in
235-
responseLBS (V1.toHttpErrorStatus err) [applicationJson] (encode err)
236-
, Throttle.throttleRate = fromIntegral $ tsRate ts
237-
, Throttle.throttlePeriod = fromIntegral $ tsPeriod ts
238-
, Throttle.throttleBurst = fromIntegral $ tsBurst ts
239-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{- | A collection of middlewares used by this edge node.
2+
@Middleware@ is a component that sits between the server and application.
3+
It can do such tasks as GZIP encoding or response caching.
4+
-}
5+
6+
module Cardano.Wallet.Server.Middlewares
7+
( withMiddlewares
8+
, throttleMiddleware
9+
) where
10+
11+
import Universum
12+
13+
import Data.Aeson (encode)
14+
import Network.Wai (Application, Middleware, responseLBS)
15+
import qualified Network.Wai.Middleware.Throttle as Throttle
16+
17+
import Cardano.Wallet.API.V1.Headers (applicationJson)
18+
import qualified Cardano.Wallet.API.V1.Types as V1
19+
20+
import Pos.Launcher.Configuration (ThrottleSettings (..))
21+
22+
-- | "Attaches" the middlewares to this 'Application'.
23+
withMiddlewares :: [Middleware] -> Application -> Application
24+
withMiddlewares = flip $ foldr ($)
25+
26+
-- | A @Middleware@ to throttle requests.
27+
throttleMiddleware :: Maybe ThrottleSettings -> Middleware
28+
throttleMiddleware Nothing app = app
29+
throttleMiddleware (Just ts) app = \req respond -> do
30+
throttler <- Throttle.initThrottler
31+
Throttle.throttle throttleSettings throttler app req respond
32+
where
33+
throttleSettings = Throttle.defaultThrottleSettings
34+
{ Throttle.onThrottled = \microsTilRetry ->
35+
let
36+
err = V1.RequestThrottled microsTilRetry
37+
in
38+
responseLBS (V1.toHttpErrorStatus err) [applicationJson] (encode err)
39+
, Throttle.throttleRate = fromIntegral $ tsRate ts
40+
, Throttle.throttlePeriod = fromIntegral $ tsPeriod ts
41+
, Throttle.throttleBurst = fromIntegral $ tsBurst ts
42+
}

wallet-new/src/Cardano/Wallet/Server/Plugins.hs

+9-18
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,24 @@ import Universum
1616

1717
import Data.Acid (AcidState)
1818

19-
import Network.Wai (Application, Middleware, responseLBS)
19+
import Network.Wai (Application, Middleware)
2020
import Network.Wai.Handler.Warp (defaultSettings)
21-
import qualified Network.Wai.Middleware.Throttle as Throttle
2221

2322
import Cardano.NodeIPC (startNodeJsIPC)
2423
import Cardano.Wallet.API as API
25-
import Cardano.Wallet.API.V1.Headers (applicationJson)
26-
import qualified Cardano.Wallet.API.V1.Types as V1
2724
import Cardano.Wallet.Kernel (DatabaseMode (..), PassiveWallet)
2825
import Cardano.Wallet.Server.CLI (NewWalletBackendParams (..),
2926
WalletBackendParams (..), getWalletDbOptions, isDebugMode,
3027
walletAcidInterval)
28+
import Cardano.Wallet.Server.Middlewares (withMiddlewares)
3129
import Cardano.Wallet.WalletLayer (ActiveWalletLayer,
3230
PassiveWalletLayer)
3331
import Pos.Chain.Update (cpsSoftwareVersion)
3432
import Pos.Crypto (ProtocolMagic)
3533
import Pos.Infra.Diffusion.Types (Diffusion (..))
3634
import Pos.Infra.Shutdown (HasShutdownContext (shutdownContext),
3735
ShutdownContext)
38-
import Pos.Launcher.Configuration (HasConfigurations,
39-
ThrottleSettings (..))
36+
import Pos.Launcher.Configuration (HasConfigurations)
4037
import Pos.Util.CompileInfo (HasCompileInfo)
4138
import Pos.Util.Wlog (logInfo, modifyLoggerName, usingLoggerName)
4239
import Pos.Web (serveDocImpl, serveImpl)
@@ -49,7 +46,6 @@ import Cardano.Wallet.Server.Plugins.AcidState
4946
(createAndArchiveCheckpoints)
5047
import qualified Cardano.Wallet.WalletLayer as WalletLayer
5148
import qualified Cardano.Wallet.WalletLayer.Kernel as WalletLayer.Kernel
52-
import Data.Aeson (encode)
5349
import qualified Data.ByteString.Char8 as BS8
5450
import qualified Servant
5551

@@ -99,11 +95,6 @@ apiServer protocolMagic (NewWalletBackendParams WalletBackendParams{..}) (passiv
9995
portCallback ctx =
10096
usingLoggerName "NodeIPC" . flip runReaderT ctx . startNodeJsIPC
10197

102-
-- | "Attaches" the middlewares to this 'Application'.
103-
withMiddlewares :: [Middleware] -> Application -> Application
104-
withMiddlewares = flip $ foldr ($)
105-
106-
10798
-- | A @Plugin@ to serve the wallet documentation
10899
docServer
109100
:: (HasConfigurations, HasCompileInfo)
@@ -145,12 +136,12 @@ acidStateSnapshots :: AcidState db
145136
-> DatabaseMode
146137
-> Plugin Kernel.WalletMode
147138
acidStateSnapshots dbRef params dbMode = pure $ \_diffusion -> do
148-
let opts = getWalletDbOptions params
149-
modifyLoggerName (const "acid-state-checkpoint-plugin") $
150-
createAndArchiveCheckpoints
151-
dbRef
152-
(walletAcidInterval opts)
153-
dbMode
139+
let opts = getWalletDbOptions params
140+
modifyLoggerName (const "acid-state-checkpoint-plugin") $
141+
createAndArchiveCheckpoints
142+
dbRef
143+
(walletAcidInterval opts)
144+
dbMode
154145

155146
-- | A @Plugin@ to store updates proposal received from the blockchain
156147
updateWatcher :: Plugin Kernel.WalletMode

0 commit comments

Comments
 (0)