snaplet-riak (empty) → 0.1.0.0
raw patch · 4 files changed
+93/−0 lines, 4 filesdep +basedep +data-lensdep +data-lens-templatesetup-changed
Dependencies added: base, data-lens, data-lens-template, mtl, riak, snap, snap-core, time, transformers
Files
- LICENSE +7/−0
- Setup.hs +2/−0
- snaplet-riak.cabal +30/−0
- src/Snap/Snaplet/Riak.hs +54/−0
+ LICENSE view
@@ -0,0 +1,7 @@+Copyright (C) 2012 Paul Wilson++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ snaplet-riak.cabal view
@@ -0,0 +1,30 @@+name: snaplet-riak+version: 0.1.0.0+synopsis: A Snaplet for the Riak database+-- description: +license: MIT+license-file: LICENSE+author: Paul Wilson+maintainer: paul@statusfailed.com+copyright: +category: Database+build-type: Simple+cabal-version: >=1.8++library+ hs-source-dirs: src++ -- exposed-modules: + -- other-modules: + Build-depends:+ base >= 4 && < 5,+ transformers >= 0.3.0 && < 0.4,+ mtl >= 2.1.2 && < 2.2,+ snap >= 0.9.2 && < 0.10,+ snap-core >= 0.9.2 && < 0.10,+ data-lens-template >= 2.1.7 && < 2.2,+ data-lens >= 2.10.2 && < 2.11,+ riak >= 0.7.0 && < 0.8,+ time >= 1.4.0 && < 1.5++ Exposed-modules: Snap.Snaplet.Riak
+ src/Snap/Snaplet/Riak.hs view
@@ -0,0 +1,54 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TemplateHaskell #-}++-- | A Snaplet for using the Riak database (via the 'Network.Riak' package)+module Snap.Snaplet.Riak+ ( RiakDB+ , withRiak+ , riakInit+ , riakCreate+ ) where++import Prelude hiding ((.))+import Control.Category ((.))+import Control.Monad.State+import Control.Monad.IO.Class++import Data.Lens.Common+import Data.Lens.Template++import Snap.Snaplet++import Data.Time.Clock+import Network.Riak+import Network.Riak.Connection.Pool++-- | Riak Snaplet state+data RiakDB = RiakDB+ { _pool :: Pool+ }++makeLens ''RiakDB++-- | Perform an action using a Riak Connection in the Riak snaplet+--+-- > result <- withRiak $ \c -> get c "myBucket" "myKey" Default+withRiak :: (MonadIO m, MonadState app m) =>+ Lens app (Snaplet RiakDB) -> (Connection -> IO a) -> m a+withRiak snaplet f = do+ c <- gets $ getL (pool . snapletValue . snaplet)+ liftIO $ withConnection c f++-- | Utility function for creating the Riak snaplet from an Initializer+makeRiak :: Initializer b v v -> SnapletInit b v+makeRiak = makeSnaplet "snaplet-riak" "Riak Snaplet." Nothing++-- | Initialize the Riak snaplet+riakInit :: Pool -> SnapletInit b RiakDB+riakInit pool = makeRiak . return $ RiakDB pool++-- | Thin wrapper around 'Network.Riak.create' to run within 'SnapletInit'+riakCreate :: Client -> Int -> NominalDiffTime -> Int -> SnapletInit b RiakDB+riakCreate c ns t nc = makeRiak $ do+ pool <- liftIO $ create c ns t nc+ return $ RiakDB pool