{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}

module Application
  ( main
  ) where

import Conduit (runResourceT)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Logger (runStderrLoggingT)
import Data.Text (Text)
import Database.Persist.Sql (runMigration, runSqlPool)
import Database.Persist.Sqlite (withSqlitePool)
import Yesod (mkYesodDispatch, warp)

import Foundation
import Handler

mkYesodDispatch "App" resourcesApp

dbName :: Text
dbName :: Text
dbName = Text
"env.db"

openConnectionCount :: Int
openConnectionCount :: Int
openConnectionCount = Int
10

main :: IO ()
main :: IO ()
main = do
  String -> IO ()
putStrLn String
"Serving application on http://localhost:3000"
  forall (m :: * -> *) a. MonadIO m => LoggingT m a -> m a
runStderrLoggingT forall a b. (a -> b) -> a -> b
$
    forall (m :: * -> *) a.
(MonadUnliftIO m, MonadLoggerIO m) =>
Text -> Int -> (Pool SqlBackend -> m a) -> m a
withSqlitePool Text
dbName Int
openConnectionCount forall a b. (a -> b) -> a -> b
$ \Pool SqlBackend
pool ->
      forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
        forall (m :: * -> *) a. MonadUnliftIO m => ResourceT m a -> m a
runResourceT forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> b -> a -> c
flip forall backend (m :: * -> *) a.
(MonadUnliftIO m, BackendCompatible SqlBackend backend) =>
ReaderT backend m a -> Pool backend -> m a
runSqlPool Pool SqlBackend
pool forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
MonadIO m =>
Migration -> ReaderT SqlBackend m ()
runMigration Migration
migrateAll
        forall site. YesodDispatch site => Int -> site -> IO ()
warp Int
3000 forall a b. (a -> b) -> a -> b
$ Pool SqlBackend -> App
App Pool SqlBackend
pool