Application.hs 1.5 KB

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