Application.hs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. {-# LANGUAGE BangPatterns #-}
  2. {-# OPTIONS_GHC -fno-warn-orphans #-}
  3. module Application
  4. ( makeApplication
  5. , makeFoundation
  6. ) where
  7. import Import
  8. import Control.Monad
  9. import Control.DeepSeq (force)
  10. import System.Random
  11. import qualified Database.Persist.Store
  12. import Database.Persist.Store (PersistValue (..))
  13. import Network.HTTP.Conduit (newManager, def)
  14. import Yesod.Default.Config
  15. import Settings
  16. getJsonR :: Handler RepJson
  17. getJsonR = jsonToRepJson $ object [("message", "Hello, World!" :: Text)]
  18. getDBR :: Handler RepJson
  19. getDBR = do
  20. !i <- liftIO $ randomRIO (1, 10000)
  21. Just o <- runDB $ get $ Key $ PersistInt64 i
  22. jsonToRepJson $ object ["id" .= i, "randomNumber" .= worldRandomNumber o]
  23. getDB2R :: Int -> Handler RepJson
  24. getDB2R n = do
  25. !is <- force . take n . randomRs (1, 10000) <$> liftIO newStdGen
  26. ns <- runDB $
  27. forM is $ \i -> do
  28. Just o <- get $ Key $ PersistInt64 i
  29. return (i, worldRandomNumber o)
  30. jsonToRepJson $ array
  31. [ object ["id" .= i, "randomNumber" .= rn] | (i, rn) <- ns ]
  32. mkYesodDispatch "App" resourcesApp
  33. makeApplication :: AppConfig DefaultEnv Extra -> IO Application
  34. makeApplication conf = makeFoundation conf >>= toWaiAppPlain
  35. makeFoundation :: AppConfig DefaultEnv Extra -> IO App
  36. makeFoundation conf = do
  37. manager <- newManager def
  38. dbconf <- withYamlEnvironment "config/mysql.yml" (appEnv conf)
  39. Database.Persist.Store.loadConfig >>=
  40. Database.Persist.Store.applyEnv
  41. p <- Database.Persist.Store.createPoolConfig (dbconf :: Settings.PersistConfig)
  42. let foundation = App conf p manager dbconf
  43. return foundation