Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace ClassyPrelude with RIO in simple #194

Open
wants to merge 2 commits into
base: simple
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/stack.yaml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be included in a template, we'll want the users of the template to check in their stack.yaml file.

dist*
static/tmp/
static/combined/
Expand Down
10 changes: 7 additions & 3 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ version: "0.0.0"
dependencies:

- base >=4.9.1.0 && <5

- yesod >=1.6 && <1.7
- yesod-core >=1.6 && <1.7
- yesod-static >=1.6 && <1.7
- yesod-form >=1.6 && <1.7
- classy-prelude >=1.5 && <1.6
- classy-prelude-conduit >=1.5 && <1.6
- classy-prelude-yesod >=1.5 && <1.6

- rio >= 0.1.8.0
- http-types
- persistent
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dependency and the next shouldn't be included in an actual simple template. I'm assuming it's here for testing/instances/etc.

- yesod-newsfeed

- bytestring >=0.10 && <0.11
- text >=0.11 && <2.0
- template-haskell
Expand Down
62 changes: 44 additions & 18 deletions src/Application.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Application
) where

import Control.Monad.Logger (liftLoc)
import Import
import Import hiding (LevelError)
import Language.Haskell.TH.Syntax (qLocation)
import Network.HTTP.Client.TLS (getGlobalManager)
import Network.Wai (Middleware)
Expand All @@ -34,6 +34,7 @@ import Network.Wai.Middleware.RequestLogger (Destination (Logger),
mkRequestLogger, outputFormat)
import System.Log.FastLogger (defaultBufSize, newStdoutLoggerSet,
toLogStr)
import Yesod (LogLevel(LevelError))

-- Import all relevant handler modules here.
-- Don't forget to add new modules to your cabal file!
Expand All @@ -50,8 +51,8 @@ mkYesodDispatch "App" resourcesApp
-- performs initialization and returns a foundation datatype value. This is also
-- the place to put your migrate statements to have automatic database
-- migrations handled by Yesod.
makeFoundation :: AppSettings -> IO App
makeFoundation appSettings = do
makeFoundation :: AppSettings -> LogFunc -> IO App
makeFoundation appSettings appLogFunc = do
-- Some basic initializations: HTTP connection manager, logger, and static
-- subsite.
appHttpManager <- getGlobalManager
Expand Down Expand Up @@ -105,10 +106,11 @@ warpSettings foundation =
getApplicationDev :: IO (Settings, Application)
getApplicationDev = do
settings <- getAppSettings
foundation <- makeFoundation settings
wsettings <- getDevSettings $ warpSettings foundation
app <- makeApplication foundation
return (wsettings, app)
crLogFunc settings $ \logFunc -> do
foundation <- makeFoundation settings logFunc
wsettings <- getDevSettings $ warpSettings foundation
app <- makeApplication foundation
return (wsettings, app)

getAppSettings :: IO AppSettings
getAppSettings = loadYamlSettings [configSettingsYml] [] useEnv
Expand All @@ -128,26 +130,29 @@ appMain = do
-- allow environment variables to override
useEnv

-- Generate the foundation from the settings
foundation <- makeFoundation settings
crLogFunc settings $ \logFunc -> do
-- Generate the foundation from the settings
foundation <- makeFoundation settings logFunc

-- Generate a WAI Application from the foundation
app <- makeApplication foundation
-- Generate a WAI Application from the foundation
app <- makeApplication foundation

-- Run the application with Warp
runSettings (warpSettings foundation) app
-- Run the application with Warp
runSettings (warpSettings foundation) app


--------------------------------------------------------------
-- Functions for DevelMain.hs (a way to run the app from GHCi)
--------------------------------------------------------------

getApplicationRepl :: IO (Int, App, Application)
getApplicationRepl = do
settings <- getAppSettings
foundation <- makeFoundation settings
wsettings <- getDevSettings $ warpSettings foundation
app1 <- makeApplication foundation
return (getPort wsettings, foundation, app1)
crLogFunc settings $ \logFunc -> do
foundation <- makeFoundation settings logFunc
wsettings <- getDevSettings $ warpSettings foundation
app1 <- makeApplication foundation
return (getPort wsettings, foundation, app1)

shutdownApp :: App -> IO ()
shutdownApp _ = return ()
Expand All @@ -159,4 +164,25 @@ shutdownApp _ = return ()

-- | Run a handler
handler :: Handler a -> IO a
handler h = getAppSettings >>= makeFoundation >>= flip unsafeHandler h
handler h = do
settings <- getAppSettings
crLogFunc settings $ \logFunc -> do
makeFoundation settings logFunc >>= flip unsafeHandler h


---------------------------------------------
-- RIO logFunc wrapper
---------------------------------------------

crLogFunc :: AppSettings -> (LogFunc -> IO a) -> IO a
crLogFunc appSettings cb = do
lo <- opts <$> logOptionsHandle stdout is_logging
withLogFunc lo cb
where
opts =
setLogVerboseFormat is_detailed
. setLogUseTime is_detailed
. setLogUseLoc is_detailed

is_logging = True
is_detailed = appDetailedRequestLogging appSettings
11 changes: 10 additions & 1 deletion src/Foundation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@

module Foundation where

import Import.NoFoundation
import Import.NoFoundation hiding (LogSource,LogLevel(..))
import Control.Monad.Logger (LogSource)
import Text.Hamlet (hamletFile)
import Text.Jasmine (minifym)
import Yesod (LogLevel(..))
import Yesod.Core.Types (Logger)
import Yesod.Default.Util (addStaticContentExternal)
import qualified Yesod.Core.Unsafe as Unsafe
import qualified Data.CaseInsensitive as CI
import qualified Data.Text.Encoding as TE


-- | run a 'RIO' function from a 'MondHandler' context
runRIOinHandler :: (MonadHandler m, HandlerSite m ~ App) => RIO App a -> m a
runRIOinHandler rio = do
a <- getYesod
liftIO $ runRIO a rio

-- | The foundation datatype for your application. This can be a good place to
-- keep settings and values requiring initialization before your application
-- starts running, such as database connections. Every handler will have
Expand All @@ -27,6 +35,7 @@ data App = App
, appStatic :: Static -- ^ Settings for static file serving.
, appHttpManager :: Manager
, appLogger :: Logger
, appLogFunc :: LogFunc
}

data MenuItem = MenuItem
Expand Down
39 changes: 32 additions & 7 deletions src/Import/NoFoundation.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Import.NoFoundation
( module Import
( module X
) where

import ClassyPrelude.Yesod as Import
import Settings as Import
import Settings.StaticFiles as Import
import Yesod.Core.Types as Import (loggerSet)
import Yesod.Default.Config2 as Import
import RIO.Yesod as X
import Data.Default as X (Default (..))
import Database.Persist.Sql as X (runMigration)
import Database.Persist.Sql as X (SqlBackend, SqlPersistT)
import Network.HTTP.Client.Conduit as X hiding (Proxy(..))
import Network.HTTP.Types as X
import Settings as X
import Settings.StaticFiles as X
import Yesod as X
hiding
( Header
, parseTime
, LogLevel(..)
, logDebug
, logDebugS
, logError
, logErrorS
, logInfo
, logInfoS
, logOther
, logOtherS
, logWarn
, logWarnS
)
import Yesod.Core.Types as X (loggerSet)
import Yesod.Default.Config2 as X
import Yesod.Feed as X
import Yesod.Static as X
20 changes: 20 additions & 0 deletions src/RIO/Yesod.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module RIO.Yesod
( module RIO
) where

import RIO
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be possible to provide a HasLogFunc instance on the App type instead.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me get back to you n that — probably on Friday.

hiding
( Handler(..)
-- , LogLevel(..)
-- , LogSource
-- , logDebug
-- , logDebugS
-- , logError
-- , logErrorS
-- , logInfo
-- , logInfoS
-- , logOther
-- , logOtherS
-- , logWarn
-- , logWarnS
)
11 changes: 8 additions & 3 deletions src/Settings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@
-- declared in the Foundation.hs file.
module Settings where

import ClassyPrelude.Yesod
import RIO.Yesod
import qualified Control.Exception as Exception
import Data.Aeson (Result (..), fromJSON, withObject, (.!=),
(.:?))
import Data.Aeson (Result (..), FromJSON(..), fromJSON,
withObject,(.:),(.!=),(.:?),Value(..))
import Data.Default (def)
import Data.FileEmbed (embedFile)
import Data.Yaml (decodeEither')
import Language.Haskell.TH.Syntax (Exp, Name, Q)
import Network.Wai.Handler.Warp (HostPreference)
import Yesod.Default.Config2 (applyEnvValue, configSettingsYml)
import Yesod.Default.Util (WidgetFileSettings, widgetFileNoReload,
widgetFileReload)
import Yesod.Core.Types (Route)
import Yesod.Static (CombineSettings,combineScripts',
combineStylesheets', Static)


-- | Runtime settings to configure this application. These settings can be
-- loaded from various sources: defaults, environment variables, config files,
Expand Down
2 changes: 1 addition & 1 deletion test/TestImport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module TestImport
) where

import Application (makeFoundation, makeLogWare)
import ClassyPrelude as X hiding (Handler)
import RIO as X hiding (Handler)
import Foundation as X
import Test.Hspec as X
import Yesod.Default.Config2 (useEnv, loadYamlSettings)
Expand Down