hedis-envy (empty) → 0.1.0.0
raw patch · 6 files changed
+170/−0 lines, 6 filesdep +basedep +envydep +hedissetup-changed
Dependencies added: base, envy, hedis, scientific, time
Files
- LICENSE +30/−0
- README.md +1/−0
- Setup.hs +2/−0
- hedis-envy.cabal +31/−0
- src/Database/Redis/Envy.hs +93/−0
- src/Database/Redis/Envy/Instance.hs +13/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright IIJ Innovation Institute Inc. (c) 2020++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of IIJ Innovation Institute Inc. nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,1 @@+# hedis-envy
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hedis-envy.cabal view
@@ -0,0 +1,31 @@+cabal-version: 2.2+name: hedis-envy+version: 0.1.0.0+description: Provides FromEnv instance for ConnectInfo of hedis+homepage: https://github.com/igrep/hedis-envy#readme+bug-reports: https://github.com/igrep/hedis-envy/issues+author: IIJ Innovation Institute, Inc.+maintainer: yuji-yamamoto@iij.ad.jp+copyright: 2020 IIJ Innovation Institute, Inc.+license: BSD-3-Clause+license-file: LICENSE+build-type: Simple+category: Database,System+extra-source-files: README.md++source-repository head+ type: git+ location: https://github.com/igrep/hedis-envy++library+ exposed-modules:+ Database.Redis.Envy+ Database.Redis.Envy.Instance+ build-depends:+ , base >=4.7 && <5+ , hedis >= 0.12+ , envy >= 2.0+ , time+ , scientific+ default-language: Haskell2010+ hs-source-dirs: src
+ src/Database/Redis/Envy.hs view
@@ -0,0 +1,93 @@+module Database.Redis.Envy+ ( connectInfoFromEnv+ , connectInfoFromEnvWithDefault+ , parsePortId+ ) where+++import Data.Foldable (asum)+import Data.Scientific (Scientific)+import Data.Time (NominalDiffTime)+import Data.Typeable (Typeable)+import qualified Database.Redis as R+import System.Envy (Parser, Var, env, envMaybe, fromVar, toVar,+ (.!=))+import Text.Read (readMaybe)+++newtype ReadShowVar a = ReadShowVar { unReadShowVar :: a }++instance (Typeable a, Show a, Read a) => Var (ReadShowVar a) where+ toVar = show . unReadShowVar+ fromVar = fmap ReadShowVar . readMaybe+++-- | Get 'R.ConnectInfo' from these environment variables:+--+-- * @REDIS_HOST@: 'R.connectHost'+-- * @REDIS_PORT_ID@: 'R.connectPort'+-- * @REDIS_AUTH@: 'R.connectAuth'+-- * @REDIS_DATABASE_INDEX@: 'R.connectDatabase'+-- * @REDIS_MAX_CONNECTIONS@: 'R.connectMaxConnections'+-- * @REDIS_MAX_IDLE_TIME@: 'R.connectMaxIdleTime'+-- * @REDIS_TIMEOUT@: 'R.connectTimeout'+--+-- The corresponding field of 'R.defaultConnectInfo' is used as the default value.+--+-- NOTE: 'R.connectTLSParams' is NOT supported.+-- This is hard to express as environment variables.+connectInfoFromEnv :: Parser R.ConnectInfo+connectInfoFromEnv = connectInfoFromEnvWithDefault R.defaultConnectInfo++-- | A variant of 'connectInfoFromEnv' which you can provide the+-- default value by yourself.+--+-- This is necessary to implement the instance of 'Env.FromEnv' for+-- 'R.ConnectInfo' correctly.+connectInfoFromEnvWithDefault :: R.ConnectInfo -> Parser R.ConnectInfo+connectInfoFromEnvWithDefault def = R.ConnInfo+ <$> envMaybe "REDIS_HOST" .!= R.connectHost def+ <*> pPortId+ <*> env "REDIS_AUTH" .!= R.connectAuth def+ <*> envMaybe "REDIS_DATABASE_INDEX" .!= R.connectDatabase def+ <*> envMaybe "REDIS_MAX_CONNECTIONS" .!= R.connectMaxConnections def+ <*>+ (fmap nominalDiffTimeFromVal <$> envMaybe "REDIS_MAX_IDLE_TIME")+ .!= R.connectMaxIdleTime def+ <*> timeout+ <*> pure (R.connectTLSParams def)+ where+ nominalDiffTimeFromVal :: ReadShowVar Scientific -> NominalDiffTime+ nominalDiffTimeFromVal = realToFrac . unReadShowVar++ timeout :: Parser (Maybe NominalDiffTime)+ timeout =+ maybe+ (R.connectTimeout def)+ (Just . nominalDiffTimeFromVal)+ <$> envMaybe "REDIS_TIMEOUT"++ pPortId =+ maybe+ (pure (R.connectPort def))+ parsePortId+ =<< envMaybe "REDIS_PORT_ID"+++-- | Parse the value of @REDIS_PORT_ID@.+-- The value must be a port number or the absolute path to the UNIX domain+-- socket.+--+-- Internally used by 'connectInfoFromEnv'+parsePortId+ :: String -- ^ Value of environment variable+ -> Parser R.PortID+parsePortId envVarVal = maybe (fail emsg) pure $ asum+ [ R.PortNumber <$> readMaybe envVarVal+ , R.UnixSocket <$> parsePath envVarVal+ ]+ where+ parsePath path@('/': _) = Just path+ parsePath _other = Nothing+ emsg =+ "The value of PortID must be a number (TCP port), or an absolute path to the UNIX domain socket!"
+ src/Database/Redis/Envy/Instance.hs view
@@ -0,0 +1,13 @@+{-# OPTIONS_GHC -Wno-orphans #-}++module Database.Redis.Envy.Instance () where+++import qualified Database.Redis as R+import Database.Redis.Envy+import System.Envy (FromEnv, fromEnv)+++instance FromEnv R.ConnectInfo where+ fromEnv Nothing = connectInfoFromEnv+ fromEnv (Just def) = connectInfoFromEnvWithDefault def