Skip to content

Commit

Permalink
Add SiteConfig, avoiding extra run* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Nov 26, 2023
1 parent add1786 commit 96dc586
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
3 changes: 2 additions & 1 deletion ema-examples/src/Ema/Example/Ex06_Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ runWithFollow ::
IO ()
runWithFollow input = do
cli <- CLI.cliAction
result <- snd <$> runSiteWithServerOpts @Route followServerOptions cli input
let cfg = SiteConfig cli followServerOptions
result <- snd <$> runSiteWith @Route cfg input
case result of
CLI.Run _ :=> Identity () ->
flip runLoggerLoggingT (CLI.getLogger cli) $
Expand Down
50 changes: 26 additions & 24 deletions ema/src/Ema/App.hs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{-# LANGUAGE AllowAmbiguousTypes #-}

module Ema.App (
SiteConfig (..),
runSite,
runSite_,
runSiteWithCli,
runSiteWithServerOpts,
runSiteWith,
) where

import Control.Concurrent (threadDelay)
import Control.Concurrent.Async (race_)
import Control.Monad.Logger (LoggingT (runLoggingT), MonadLoggerIO (askLoggerIO), logInfoNS, logWarnNS)
import Control.Monad.Logger.Extras (runLoggerLoggingT)
import Data.Default (def)
import Data.Default (Default, def)
import Data.Dependent.Sum (DSum ((:=>)))
import Data.LVar qualified as LVar
import Data.Some (Some (Some))
Expand All @@ -24,6 +24,18 @@ import Ema.Server qualified as Server
import Ema.Site (EmaSite (SiteArg, siteInput), EmaStaticSite)
import System.Directory (getCurrentDirectory)

data SiteConfig r = SiteConfig
{ siteConfigCli :: CLI.Cli
, siteConfigServerOpts :: Server.EmaServerOptions r
}

instance Default (SiteConfig r) where
def =
SiteConfig
{ siteConfigCli = def
, siteConfigServerOpts = def
}

{- | Run the given Ema site,
Takes as argument the associated `SiteArg`.
Expand All @@ -39,7 +51,8 @@ runSite ::
IO [FilePath]
runSite input = do
cli <- CLI.cliAction
result <- snd <$> runSiteWithCli @r cli input
let cfg = SiteConfig cli def
result <- snd <$> runSiteWith @r cfg input
case result of
CLI.Run _ :=> Identity () ->
flip runLoggerLoggingT (getLogger cli) $
Expand All @@ -51,37 +64,26 @@ runSite input = do
runSite_ :: forall r. (Show r, Eq r, EmaStaticSite r) => SiteArg r -> IO ()
runSite_ = void . runSite @r

{- | Like @runSite@ but takes the CLI action. Also returns more information.
{- | Like @runSite@ but takes custom @SiteConfig@.
Useful if you are handling the CLI arguments yourself.
Useful if you are handling the CLI arguments yourself and/or customizing the
server websocket handler.
Use "void $ Ema.runSiteWithCli def ..." if you are running live-server only.
Use "void $ Ema.runSiteWith def ..." if you are running live-server only.
-}
runSiteWithCli ::
forall r.
(Show r, Eq r, EmaStaticSite r) =>
CLI.Cli ->
SiteArg r ->
IO
( -- The initial model value.
RouteModel r
, DSum CLI.Action Identity
)
runSiteWithCli = runSiteWithServerOpts @r def

-- | Like @runSiteWithCli@ but takes Ema server options.
runSiteWithServerOpts ::
runSiteWith ::
forall r.
(Show r, Eq r, EmaStaticSite r) =>
Server.EmaServerOptions r ->
CLI.Cli ->
SiteConfig r ->
SiteArg r ->
IO
( -- The initial model value.
RouteModel r
, DSum CLI.Action Identity
)
runSiteWithServerOpts opts cli siteArg = do
runSiteWith cfg siteArg = do
let opts = siteConfigServerOpts cfg
cli = siteConfigCli cfg
flip runLoggerLoggingT (getLogger cli) $ do
cwd <- liftIO getCurrentDirectory
logInfoNS "ema" $ "Launching Ema under: " <> toText cwd
Expand Down

0 comments on commit 96dc586

Please sign in to comment.