|
@@ -3,13 +3,15 @@
|
|
|
|
|
|
module Main where
|
|
module Main where
|
|
|
|
|
|
|
|
+import Control.Concurrent.Async (mapConcurrently)
|
|
import Control.Monad.IO.Class
|
|
import Control.Monad.IO.Class
|
|
import Data.Aeson hiding (json)
|
|
import Data.Aeson hiding (json)
|
|
-import Data.Maybe (listToMaybe)
|
|
|
|
|
|
+import Data.Maybe (catMaybes, listToMaybe)
|
|
import qualified Data.Text as T
|
|
import qualified Data.Text as T
|
|
import qualified Database.PostgreSQL.Simple as PG
|
|
import qualified Database.PostgreSQL.Simple as PG
|
|
import Database.PostgreSQL.Simple.FromRow
|
|
import Database.PostgreSQL.Simple.FromRow
|
|
import GHC.Exts
|
|
import GHC.Exts
|
|
|
|
+import Network.HTTP.Types.Status
|
|
import System.Random
|
|
import System.Random
|
|
import Web.Spock.Safe
|
|
import Web.Spock.Safe
|
|
|
|
|
|
@@ -26,14 +28,10 @@ instance ToJSON World where
|
|
, "randomNumber" .= _randomNumberW w
|
|
, "randomNumber" .= _randomNumberW w
|
|
]
|
|
]
|
|
|
|
|
|
|
|
+
|
|
instance FromRow World where
|
|
instance FromRow World where
|
|
fromRow = World <$> field <*> field
|
|
fromRow = World <$> field <*> field
|
|
|
|
|
|
-data Fortune = Fortune
|
|
|
|
- { _idF :: Integer
|
|
|
|
- , _messageF :: T.Text
|
|
|
|
- } deriving (Show)
|
|
|
|
-
|
|
|
|
|
|
|
|
fetchWorldById :: Int -> PG.Connection -> IO (Maybe World)
|
|
fetchWorldById :: Int -> PG.Connection -> IO (Maybe World)
|
|
fetchWorldById i c =
|
|
fetchWorldById i c =
|
|
@@ -42,6 +40,12 @@ fetchWorldById i c =
|
|
(PG.Only i)
|
|
(PG.Only i)
|
|
|
|
|
|
|
|
|
|
|
|
+data Fortune = Fortune
|
|
|
|
+ { _idF :: Integer
|
|
|
|
+ , _messageF :: T.Text
|
|
|
|
+ } deriving (Show)
|
|
|
|
+
|
|
|
|
+
|
|
dbConn :: PoolOrConn PG.Connection
|
|
dbConn :: PoolOrConn PG.Connection
|
|
dbConn =
|
|
dbConn =
|
|
PCConn (ConnBuilder
|
|
PCConn (ConnBuilder
|
|
@@ -59,16 +63,26 @@ dbConn =
|
|
main :: IO ()
|
|
main :: IO ()
|
|
main =
|
|
main =
|
|
runSpock 3000 $ spock (defaultSpockCfg Nothing dbConn ()) $
|
|
runSpock 3000 $ spock (defaultSpockCfg Nothing dbConn ()) $
|
|
|
|
+ -- | Test 1: JSON serialization
|
|
do get "json" $
|
|
do get "json" $
|
|
- -- | Test 1: JSON serialization
|
|
|
|
json $ Object (fromList [("message", "Hello, World!")])
|
|
json $ Object (fromList [("message", "Hello, World!")])
|
|
- get "db" $ do
|
|
|
|
- -- | Test 2: Single database query
|
|
|
|
- rand <- liftIO $ randomRIO (1, 10000)
|
|
|
|
- maybeWorld <- runQuery $ fetchWorldById rand
|
|
|
|
- case maybeWorld of
|
|
|
|
- Just w -> json w
|
|
|
|
- Nothing -> text "World not found."
|
|
|
|
|
|
+ -- | Test 2: Single database query
|
|
|
|
+ get "db" $
|
|
|
|
+ do rand <- liftIO $ randomRIO (1, 10000)
|
|
|
|
+ maybeWorld <- runQuery $ fetchWorldById rand
|
|
|
|
+ case maybeWorld of
|
|
|
|
+ Just w -> json w
|
|
|
|
+ Nothing -> setStatus status404 >> text "World not found."
|
|
|
|
+ -- | Test 3: Multiple database queries
|
|
|
|
+ get "queries" $
|
|
|
|
+ do queries <- max 1 . min 500 <$> param' "queries"
|
|
|
|
+ let runSQL core = runSpockIO core . runQuery . fetchWorldById =<< randomRIO (1, 10000)
|
|
|
|
+ spockCore <- getSpockHeart
|
|
|
|
+ maybeWorlds <- liftIO $ mapConcurrently (\_ -> runSQL spockCore) ([1..queries] :: [Int])
|
|
|
|
+ json $ catMaybes maybeWorlds
|
|
|
|
+ -- | Test 4: Fortunes
|
|
|
|
+ get "fortune" $
|
|
|
|
+ do liftIO $ putStrLn "test"
|
|
|
|
+ -- | Test 6: Plain text
|
|
get "plaintext" $
|
|
get "plaintext" $
|
|
- -- | Test 6: Plain text
|
|
|
|
text "Hello, World!"
|
|
text "Hello, World!"
|