Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Started working on the "Setting up the MongoDB Extension" section

  • Loading branch information...
commit c8f581fa9a68a829062dee9d8375bc01c593ef57 1 parent 48bae69
@Palmik authored
View
30 TUTORIAL.md
@@ -62,4 +62,32 @@ Our view resides in the [`resources/templates`](resources/templates) directory.
In Snap & Heist terms, the view consists of the Heist templates where we call the controller's splices.
## Down to Bussines
-### Setting up the MongoDB Extension
+### Setting up the MongoDB Extension
+
+The Snap.Extension.MongoDB is Snap extension utilizing Snap's extension interface.
+It's installed analogicaly to any other Snap's extension.
+
+In the [Application.hs](src/Application.hs):
+
+First, we import the module:
+
+ import Snap.Extension.DB.MongoDB
+
+Then we make it part of our Application's state along with Heist:
+
+ data ApplicationState = ApplicationState
+ { templateState :: HeistState Application
+ , databaseState :: MongoDBState
+ }
+
+ instance HasMongoDBState ApplicationState where
+ getMongoDBState = databaseState
+ setMongoDBState s a = a { databaseState = s }
+
+And finally call the extension's initializer in the application's initializer:
+
+ applicationInitializer :: Initializer ApplicationState
+ applicationInitializer = do
+ heist <- heistInitializer "resources/templates"
+ database <- mongoDBInitializer (Host "127.0.0.1" $ PortNumber 27017) 1 "pastie"
+ return $ ApplicationState heist database
View
3  log/access.log
@@ -780,3 +780,6 @@
127.0.0.1 - - [15/Apr/2011:23:01:53 +0200] "GET /paste/4da74031-900faa1d48000000 HTTP/1.1" 200 - "http://0.0.0.0:8000/pastes" "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"
127.0.0.1 - - [15/Apr/2011:23:01:53 +0200] "GET /favicon.ico HTTP/1.1" 404 3 - "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"
127.0.0.1 - - [15/Apr/2011:23:02:08 +0200] "GET /favicon.ico HTTP/1.1" 404 3 - "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"
+127.0.0.1 - - [19/Apr/2011:22:12:24 +0200] "GET / HTTP/1.1" 200 - - "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"
+127.0.0.1 - - [19/Apr/2011:22:12:26 +0200] "GET /pastes HTTP/1.1" 200 - "http://0.0.0.0:8000/" "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"
+127.0.0.1 - - [19/Apr/2011:22:12:26 +0200] "GET /favicon.ico HTTP/1.1" 404 3 - "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"
View
3  log/error.log
@@ -234,3 +234,6 @@
[15/Apr/2011:22:59:00 +0200] Server.httpServe: SHUTDOWN
[15/Apr/2011:22:59:00 +0200] Server.httpServe: BACKEND STOPPED
[15/Apr/2011:22:59:00 +0200] Server.httpServe: START (EventLoopSimple)
+[19/Apr/2011:22:12:15 +0200] Server.httpServe: START (EventLoopSimple)
+[19/Apr/2011:22:15:13 +0200] Server.httpServe: SHUTDOWN
+[19/Apr/2011:22:15:13 +0200] Server.httpServe: BACKEND STOPPED
View
4 src/Model/Paste.hs
@@ -45,8 +45,8 @@ makePaste t c d l = Paste (RecKey Nothing) t c d l
-- | Returns all pastes in descending order by date
getRecentPastes :: Application [Paste]
getRecentPastes = do
- res <- withDB $ rest =<< (find (select [] pastesTable) {sort = ["$natural" =: (-1 :: Int)]})
- return $ either (const []) (fromDocList) res
+ res <- withDB $ restADT =<< (find (select [] pastesTable) {sort = ["$natural" =: (-1 :: Int)]})
+ return $ either (const []) id res
-- | Returns Pastes's ObjectId as a Text
pasteIDText :: Paste -> Maybe Text
View
16 src/Model/Utils.hs
@@ -6,13 +6,12 @@ module Model.Utils
, insertADT_
, insertManyADT
, insertManyADT_
+ , restADT
+ , nextNADT
+ , nextADT
) where
-import Control.Monad (liftM)
import Data.Maybe
-import qualified Data.Text.Encoding as T (decodeUtf8)
-import qualified Data.Text as T
-import Data.Text (Text)
import Snap.Extension.DB.MongoDB
import Snap.Extension.DB.MongoDB.Generics
@@ -31,3 +30,12 @@ insertManyADT c = insertMany c . map toDoc
insertManyADT_ :: (Regular a, ToDoc (PF a), DbAccess m) => Collection -> [a] -> m ()
insertManyADT_ c adts = insertManyADT c adts >> return ()
+
+restADT :: (Regular a, FromDoc (PF a), DbAccess m) => Cursor -> m [a]
+restADT c = rest c >>= return . fromDocList
+
+nextNADT :: (Regular a, FromDoc (PF a), DbAccess m) => Int -> Cursor -> m [a]
+nextNADT n c = nextN n c >>= return . fromDocList
+
+nextADT :: (Regular a, FromDoc (PF a), DbAccess m) => Cursor -> m (Maybe a)
+nextADT c = next c >>= return . (maybe Nothing fromDoc)
Please sign in to comment.
Something went wrong with that request. Please try again.