-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mongo.hs
32 lines (26 loc) · 1.18 KB
/
Mongo.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{-# LANGUAGE OverloadedStrings, ExtendedDefaultRules #-}
import qualified Data.Text.Lazy as T
import Web.Wheb
import Web.Wheb.Plugins.Mongo
data MyApp = MyApp MongoContainer
data MyRequestState = MyRequestState
instance MongoApp MyApp where
getMongoContainer (MyApp mc) = mc
homePage :: WhebHandler MyApp MyRequestState
homePage = do
teams <- runAction $ do
delete (select [] "team")
insertMany "team" [
["name" =: "Yankees", "home" =: ["city" =: "New York", "state" =: "NY"], "league" =: "American"],
["name" =: "Mets", "home" =: ["city" =: "New York", "state" =: "NY"], "league" =: "National"],
["name" =: "Phillies", "home" =: ["city" =: "Philadelphia", "state" =: "PA"], "league" =: "National"],
["name" =: "Red Sox", "home" =: ["city" =: "Boston", "state" =: "MA"], "league" =: "American"] ]
rest =<< find (select [] "team") {sort = ["home.city" =: 1]}
text $ T.intercalate " | " $ map spack teams
main :: IO ()
main = do
opts <- generateOptions $ do
addGET "." rootPat $ homePage
mongo <- initMongo "127.0.0.1:27017" "master"
return (MyApp mongo, MyRequestState)
runWhebServer opts