Skip to content

Commit

Permalink
fixup! [#25] Creating one connection manager for all BackendEffect
Browse files Browse the repository at this point in the history
…actions
  • Loading branch information
DK318 committed Apr 4, 2022
1 parent 68354a9 commit 42df855
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 26 deletions.
16 changes: 10 additions & 6 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ import qualified Entry as E
import qualified Coffer.Directory as Dir
import Config (configCodec, Config (..))
import Entry (path, Entry)
import Polysemy.State (State, evalState)
import Network.HTTP.Client (Manager)
import Network.HTTP.Client (Manager, defaultManagerSettings, newManager)
import Polysemy.Reader (Reader, runReader)
import Network.HTTP.Client.TLS (tlsManagerSettings)

runBackendIO
:: Sem '[BackendEffect, Error CofferError, State (Maybe Manager), Embed IO, Final IO ] a
:: (Manager, Manager)
-> Sem '[BackendEffect, Error CofferError, Reader (Manager, Manager), Embed IO, Final IO ] a
-> IO a
runBackendIO action =
runBackendIO managers action =
runBackend action
& errorToIOFinal @CofferError
& evalState Nothing
& runReader managers
& embedToFinal @IO
& runFinal
>>= \case
Expand All @@ -61,7 +63,9 @@ main = do
let someCommand = oSomeCommand options
let configPath = oConfigPath options
config <- readConfig configPath
runBackendIO do
defaultManager <- newManager defaultManagerSettings
tlsManager <- newManager tlsManagerSettings
runBackendIO (defaultManager, tlsManager) do
case someCommand of
SomeCommand cmd@CmdView{} -> do
runCommand config cmd >>= \case
Expand Down
1 change: 1 addition & 0 deletions coffer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ executable coffer
, coffer
, fmt
, http-client
, http-client-tls
, lens
, optparse-applicative
, polysemy
Expand Down
4 changes: 2 additions & 2 deletions lib/Backend.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import Polysemy
import Coffer.Path (EntryPath, Path)
import BackendName (BackendName)
import Network.HTTP.Client (Manager)
import Polysemy.State (State)
import Polysemy.Reader (Reader)

type Effects r = (Member (Embed IO) r, Member (State (Maybe Manager)) r, Member (Error CofferError) r)
type Effects r = (Member (Embed IO) r, Member (Reader (Manager, Manager)) r, Member (Error CofferError) r)

class Show a => Backend a where
_name :: a -> BackendName
Expand Down
23 changes: 5 additions & 18 deletions lib/Backend/Vault/Kv.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import qualified Toml

import Control.Monad (void)
import Servant.Client (BaseUrl (BaseUrl), Scheme (Https, Http), mkClientEnv, ClientError (..), parseBaseUrl, showBaseUrl, ClientEnv)
import Network.HTTP.Client.TLS (tlsManagerSettings)
import Network.HTTP.Client (newManager, defaultManagerSettings, Manager, ManagerSettings)
import Polysemy.Error (Error, throw)
import Toml (TomlCodec)
import GHC.Generics (Generic)
Expand All @@ -42,7 +40,7 @@ import Data.Either.Extra (eitherToMaybe, maybeToEither)
import Data.Text (Text)
import BackendName (BackendName, backendNameCodec)
import Coffer.Util (didimatch)
import Polysemy.State (get, put)
import Polysemy.Reader (ask)

data VaultKvBackend =
VaultKvBackend
Expand Down Expand Up @@ -88,25 +86,14 @@ data CofferSpecials =
deriving anyclass (A.ToJSON, A.FromJSON)
makeLensesWith abbreviatedFields ''CofferSpecials

getManager :: Effects r => ManagerSettings -> Sem r Manager
getManager settings = do
managerMb <- get @(Maybe Manager)
case managerMb of
Nothing -> do
manager <- embed $ newManager settings
put (Just manager)
pure manager
Just manager -> pure manager

getEnv :: Effects r => VaultKvBackend -> Sem r ClientEnv
getEnv backend =
getEnv backend = do
(defaultManager, tlsManager) <- ask
case url of
(BaseUrl Http _ _ _) -> do
manager <- getManager defaultManagerSettings
pure $ mkClientEnv manager url
pure $ mkClientEnv defaultManager url
(BaseUrl Https _ _ _) -> do
manager <- getManager tlsManagerSettings
pure $ mkClientEnv manager url
pure $ mkClientEnv tlsManager url
where
url = vbAddress backend

Expand Down
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ executables:
- coffer
- fmt
- http-client
- http-client-tls
- lens
- optparse-applicative
- polysemy
Expand Down

0 comments on commit 42df855

Please sign in to comment.