packages feed

hedis-config (empty) → 0.0.1

raw patch · 6 files changed

+181/−0 lines, 6 filesdep +aesondep +basedep +hedissetup-changed

Dependencies added: aeson, base, hedis, scientific, text, time

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Aleksey Uimanov++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 Aleksey Uimanov 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,30 @@+# How++Just copy this++```yaml+host: localhost                 # host name or address+port: 6379                      # you can specify either port+# socket: /run/redis.socket     # or unix socket path+# service: redis                # or service name+password: "pass"                # if not specified then no password used+database: 0                     # database number to connect to+max-connections: 5              # max connections in pool+max-idle-time: 30               # keep connection open for 30 seconds+```++to your config file, then++```haskell+import Data.Yaml+import Database.Redis.Config++main :: IO ()+main = do+    conn <- decodeFile "config/redis.yml" >>= \case+        Nothing -> fail "Could not parse redis.conf"+        Just conf -> connectRedis conf+    run conn+```++Thats all, you already working with Redis connection
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ examples/redis.yml view
@@ -0,0 +1,8 @@+host: localhost                 # host name or address+port: 6379                      # you can specify either port+# socket: /run/redis.socket     # or unix socket path+# service: redis                # or service name+password: "pass"                # if not specified then no password used+database: 0                     # database number to connect to+max-connections: 5              # max connections in pool+max-idle-time: 30               # keep connection open for 30 seconds
+ hedis-config.cabal view
@@ -0,0 +1,47 @@+name:                hedis-config+version:             0.0.1+synopsis:            Easy trivial configuration for Redis+description: Datatype to parse redis connection settings from file like+  .+  > host: localhost                 # host name or address+  > port: 6379                      # you can specify either port+  > # socket: /run/redis.socket     # or unix socket path+  > # service: redis                # or service name+  > password: "pass"                # if not specified then no password used+  > database: 0                     # database number to connect to+  > max-connections: 5              # max connections in pool+  > max-idle-time: 30               # keep connection open for 30 seconds+++license:             BSD3+license-file:        LICENSE+author:              Aleksey Uimanov+maintainer:          s9gf4ult@gmail.com+category:            Database+build-type:          Simple+cabal-version:       >=1.10++extra-source-files: examples/redis.yml+                  , README.md++homepage: https://bitbucket.org/s9gf4ult/hedis-config+source-repository head+  type: git+  location: git@bitbucket.org:s9gf4ult/hedis-config.git++library+  default-language:    Haskell2010+  hs-source-dirs:      src+  default-extensions:  OverloadedStrings+                     , RecordWildCards++  build-depends:       base >=4.6 && <4.8+                     , aeson+                     , hedis >= 0.6+                     , scientific+                     , text+                     , time++  exposed-modules:     Database.Redis.Config++  ghc-options: -Wall
+ src/Database/Redis/Config.hs view
@@ -0,0 +1,64 @@+module Database.Redis.Config+       ( RedisConfig(..)+       , connectRedis+       ) where++import Control.Applicative+import Data.Aeson+import Data.Scientific+import Data.Time+import Database.Redis++import qualified Data.Aeson.Types as A+import qualified Data.Text.Encoding as T+++{- | Wrapper around 'ConnectInfo' to parse connection settings from+JSON-like values. All fields are optional, defaults are get from+'defaultConnectInfo'++Here is YAML example of config:++@+host: localhost                 # host name or address+port: 6379                      # you can specify either port+\# socket: \/run\/redis.socket     # or unix socket path+\# service: redis                # or service name+password: "pass"                # if not specified then no password used+database: 0                     # database number to connect to+max-connections: 5              # max 5 connections in pool+max-idle-time: 30               # keep connection open for 30 seconds+@++-}++newtype RedisConfig =+    RedisConfig+    { getConnectInfo :: ConnectInfo+    }+++parsePort :: Object -> A.Parser (Maybe PortID)+parsePort o =+    optional+    $   (fmap (\a -> PortNumber $ floor (a :: Scientific)) (o .: "port"))+    <|> (fmap UnixSocket (o .: "socket"))+    <|> (fmap Service (o .: "service"))+++instance FromJSON RedisConfig where+    parseJSON v = RedisConfig <$> withObject "RedisConfig" go v+      where+        go o =+            ConnInfo+            <$> (o .:? "host" .!= connectHost defaultConnectInfo)+            <*> (parsePort o .!= connectPort defaultConnectInfo)+            <*> (fmap (fmap T.encodeUtf8) (o .:? "password"))+            <*> (o .:? "database" .!= (connectDatabase defaultConnectInfo))+            <*> (o .:? "max-connections" .!= (connectMaxConnections defaultConnectInfo))+            <*> (fmap (fmap (realToFrac :: Scientific -> NominalDiffTime)) (o .:? "max-idle-time") .!= (connectMaxIdleTime defaultConnectInfo))+++-- | Open redis connection+connectRedis :: RedisConfig -> IO Connection+connectRedis = connect . getConnectInfo