<commit_before>{-# LANGUAGE EmptyDataDecls             #-}
{-# LANGUAGE RankNTypes                 #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE QuasiQuotes                #-}
{-# LANGUAGE TemplateHaskell            #-}
{-# LANGUAGE TypeFamilies               #-}

module Main where

import           Control.Applicative        (Applicative)
import           Control.Monad.IO.Class     (MonadIO, liftIO)
import           Control.Monad.Trans.Reader (ReaderT)
import           Control.Monad.Reader.Class (MonadReader)
import           Control.Monad.Logger       (runStdoutLoggingT)
import           Data.Monoid ((<>))
import qualified Data.Text.Lazy as T

import           Data.Pool (Pool)
import           Database.Persist
import           Database.Persist.Postgresql as DB
import           Database.Persist.TH

import           Web.Scotty as S
import           Web.Scotty.Trans (scottyT)
import           Network.Wai.Middleware.RequestLogger(logStdoutDev)

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
    name String
    age Int Maybe
    deriving Show
BlogPost
    title String
    authorId PersonId
    deriving Show
|]

data Config = Config { getPool :: ConnectionPool }

newtype ConfigM a = ConfigM
    { runConfigM :: ReaderT Config IO a
    } deriving (Applicative, Functor, Monad, 
                MonadIO, MonadReader Config)

connStr :: ConnectionString
connStr = "host=localhost dbname=perscotty user=test password=<PASSWORD> port=5432"

main :: IO ()
main = do
    pool <- runStdoutLoggingT $ createPostgresqlPool connStr 10
    runDb pool doMigrations 
    runDb pool doDbStuff 
    scottyT 3000 id id $ do
        middleware logStdoutDev
        S.get "/" $ S.html "Hello World"
        S.get "/posts" $ do
            posts <- runDb pool (selectList [] [])
            html ("Posts!" <> T.pack (show $ length (posts :: [Entity BlogPost])))
        S.get "/posts/:id" $ do
            postId <- S.param "id"<commit_msg>Add config monad to app... type errors!<commit_after>{-# LANGUAGE EmptyDataDecls             #-}
{-# LANGUAGE RankNTypes                 #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE QuasiQuotes                #-}
{-# LANGUAGE TemplateHaskell            #-}
{-# LANGUAGE TypeFamilies               #-}

module Main where

import           Control.Applicative        (Applicative)
import           Control.Monad.IO.Class     (MonadIO, liftIO)
import           Control.Monad.Trans.Reader (ReaderT, runReaderT)
import           Control.Monad.Reader.Class (MonadReader)
import           Control.Monad.Logger       (runStdoutLoggingT)
import           Data.Monoid ((<>))
import qualified Data.Text.Lazy as T

import           Data.Pool (Pool)
import           Database.Persist
import           Database.Persist.Postgresql as DB
import           Database.Persist.TH

import           Web.Scotty as S
import           Web.Scotty.Trans (scottyT)
import           Network.Wai.Middleware.RequestLogger(logStdoutDev)

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
    name String
    age Int Maybe
    deriving Show
BlogPost
    title String
    authorId PersonId
    deriving Show
|]

data Config = Config { getPool :: ConnectionPool }

newtype ConfigM a = ConfigM
    { runConfigM :: ReaderT Config IO a
    } deriving (Applicative, Functor, Monad, 
                MonadIO, MonadReader Config)

connStr :: ConnectionString
connStr = "host=localhost dbname=perscotty user=test password=<PASSWORD> port=5432"

main :: IO ()
main = do
    pool <- runStdoutLoggingT $ createPostgresqlPool connStr 10
    let cfg = Config pool
    let r m = runReaderT (runConfigM m) cfg
    runDb pool doMigrations 
    runDb pool doDbStuff 
    scottyT 3000 r r $ do
        middleware logStdoutDev
        S.get "/" $ S.html "Hello World"
        S.get "/posts" $ do
            posts <- runDb pool (selectList [] [])
            html ("Posts!" <> T.pack (show $ length (posts :: [Entity BlogPost])))
        S.get "/posts/:id" $ do
            postId <- S.param "id"
{'fraction_non_alphanumeric': 0.09651898734177215, 'fraction_numerical': 0.0045207956600361665, 'mean_word_length': 2.201881331403763, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2569051', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1354, 'n_words': 345}