snaplet-redis 0.1.0.3 → 0.1.1
raw patch · 2 files changed
+54/−4 lines, 2 filesdep ~lensdep ~snap
Dependency ranges changed: lens, snap
Files
- snaplet-redis.cabal +3/−3
- src/Snap/Snaplet/RedisDB.hs +51/−1
snaplet-redis.cabal view
@@ -1,5 +1,5 @@ Name: snaplet-redis-Version: 0.1.0.3+Version: 0.1.1 Synopsis: Redis support for Snap Framework Description: This package provides a snaplet which exposes interface to Redis in-memory key-value storage as@@ -30,8 +30,8 @@ Build-depends: base >= 4 && < 5,- lens == 3.8.*,+ lens >= 3.8 && < 3.10, hedis == 0.6.*, mtl >= 2 && < 3,- snap == 0.11.*,+ snap >= 0.11 && < 0.13, transformers == 0.3.*
src/Snap/Snaplet/RedisDB.hs view
@@ -11,7 +11,8 @@ module Snap.Snaplet.RedisDB (RedisDB , runRedisDB- , redisDBInit)+ , redisDBInit+ , redisDBConf) where @@ -19,6 +20,9 @@ import Control.Monad.State import Database.Redis+import Network.Socket (PortNumber(..))+import Data.Configurator as C+import Data.Maybe import Snap.Snaplet @@ -44,6 +48,52 @@ c <- gets $ view (snaplet . snapletValue . connection) liftIO $ runRedis c action ++------------------------------------------------------------------------------+-- | Make RedisDB snaplet and initialize database connection from+-- snaplet config file. Options are read from the "redis" section of+-- the application config (e.g. ./devel.cfg) or from the main section+-- of the Redis snaplet config (e.g. ./snaplets/redis/devel.cfg).+--+-- Every field is optional and defaults to defaultConnectInfo values.+-- +-- > redis {+-- > host = "192.168.0.42"+-- > port = 31415+-- > auth = "i am so secret"+-- > max_connections = 1+-- > max_idle_time = 0.5+-- > }+--+-- > appInit :: SnapletInit MyApp MyApp+-- > appInit = makeSnaplet "app" "App with Redis child snaplet" Nothing $+-- > do+-- > d <- nestSnaplet "redis" database redisDBInitConf+-- > return $ MyApp d+redisDBInitConf :: SnapletInit b RedisDB+redisDBInitConf = makeSnaplet "redis" "Redis snaplet." Nothing $ do+ config <- getSnapletUserConfig++ connInfo <- liftIO $ do+ cHost <- C.lookup config "host"+ cPort <- C.lookup config "port"+ cAuth <- C.lookup config "auth"+ cCons <- C.lookup config "max_connections"+ cIdle <- C.lookup config "max_idle_time"++ let def = defaultConnectInfo+ return $ def { connectHost = fromMaybe (connectHost def) cHost+ , connectPort =+ maybe (connectPort def) (PortNumber . PortNum) cPort+ , connectAuth = cAuth+ , connectMaxConnections =+ fromMaybe (connectMaxConnections def) cCons+ , connectMaxIdleTime =+ maybe (connectMaxIdleTime def) (fromRational) cIdle+ }++ conn <- liftIO $ connect connInfo+ return $ RedisDB conn ------------------------------------------------------------------------------ -- | Make RedisDB snaplet and initialize database connection.