Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit: wrapper snaplet for redis library.

  • Loading branch information...
commit 00b3938e13fd285e1d8483bde1887c2cb1706ff5 0 parents
@dzhus authored
Showing with 163 additions and 0 deletions.
  1. +30 −0 LICENSE
  2. +70 −0 RedisDB.hs
  3. +2 −0  Setup.hs
  4. +61 −0 snap-redis.cabal
30 LICENSE
@@ -0,0 +1,30 @@
+Copyright (c)2012, Dmitry Dzhus
+
+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 Dmitry Dzhus 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.
70 RedisDB.hs
@@ -0,0 +1,70 @@
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE TemplateHaskell #-}
+
+{-|
+
+Redis DB snaplet.
+
+-}
+
+module Snap.Snaplet.RedisDB (RedisDB
+ , withRedisDB
+ , redisDBInit)
+where
+
+import Prelude hiding ((.))
+import Control.Category ((.))
+import Control.Monad.CatchIO
+import Control.Monad.State
+import Control.Monad.Trans
+
+import Data.Lens.Common
+import Data.Lens.Template
+import Data.Text (Text)
+
+import Data.Pool
+import Database.Redis.Redis
+
+import Data.Time.Clock
+
+import Snap.Core
+import Snap.Snaplet
+
+
+------------------------------------------------------------------------------
+-- | Description text used in redisDBInit as makeSnaplet argument.
+description :: Text
+description = "Redis snaplet."
+
+keepAlive :: NominalDiffTime
+keepAlive = 60
+
+poolSize = 5
+subpoolSize = 5
+
+------------------------------------------------------------------------------
+-- | Snaplet's data type. DB connection pool is stored.
+data RedisDB = RedisDB
+ { _dbPool :: Pool Redis
+ }
+
+makeLens ''RedisDB
+
+------------------------------------------------------------------------------
+-- | Perform action using Redis connection from RedisDB snaplet pool.
+--
+-- @todo Implement WithRedis instance for apps with this.
+withRedisDB :: (MonadCatchIO m, MonadState app m) => Lens app (Snaplet RedisDB) -> (Redis -> m b) -> m b
+withRedisDB snaplet action = do
+ p <- gets $ getL (dbPool . snapletValue . snaplet)
+ withResource p action
+
+
+------------------------------------------------------------------------------
+-- | Make RedisDB snaplet and initialize database connection.
+redisDBInit :: String -> String -> SnapletInit b RedisDB
+redisDBInit host port = makeSnaplet "snaplet-redis" description Nothing $ do
+ pool <- liftIO $
+ createPool (connect host port) disconnect poolSize keepAlive subpoolSize
+ return $ RedisDB pool
2  Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
61 snap-redis.cabal
@@ -0,0 +1,61 @@
+-- snap-redis.cabal auto-generated by cabal init. For additional
+-- options, see
+-- http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#pkg-descr.
+-- The name of the package.
+Name: snap-redis
+
+-- The package version. See the Haskell package versioning policy
+-- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for
+-- standards guiding when and how versions should be incremented.
+Version: 0.1
+
+-- A short (one-line) description of the package.
+Synopsis: Redis support for Snap framework
+
+-- A longer description of the package.
+-- Description:
+
+-- URL for the project homepage or repository.
+Homepage: https://github.com/dzhus/snap-redis/
+
+-- The license under which the package is released.
+License: BSD3
+
+-- The file containing the license text.
+License-file: LICENSE
+
+-- The package author(s).
+Author: Dmitry Dzhus
+
+-- An email address to which users can send suggestions, bug reports,
+-- and patches.
+Maintainer: dima@dzhus.org
+
+-- A copyright notice.
+-- Copyright:
+
+Category: Web
+
+Build-type: Simple
+
+-- Extra files to be distributed with the package, such as examples or
+-- a README.
+-- Extra-source-files:
+
+-- Constraint on the version of Cabal needed to build this package.
+Cabal-version: >=1.2
+
+
+Library
+ -- Modules exported by the library.
+ -- Exposed-modules:
+
+ -- Packages needed in order to build this package.
+ -- Build-depends:
+
+ -- Modules not exported by this package.
+ -- Other-modules:
+
+ -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.
+ -- Build-tools:
+
Please sign in to comment.
Something went wrong with that request. Please try again.