module Happstack.Server.Plugins.Dynamic
( PluginHandle
, initPlugins
, withServerPart
, withServerPart_
) where
import Control.Monad.Trans (MonadIO(liftIO))
import Language.Haskell.TH (ExpQ, appE, varE)
import Language.Haskell.TH.Syntax (Name)
import Happstack.Plugins.Dynamic (initPlugins)
import Happstack.Plugins.LiftName (liftName)
import Happstack.Plugins.Plugins (PluginHandle, funcTH)
import Happstack.Server (ServerMonad, FilterMonad, WebMonad, Response, internalServerError, escape, toResponse)
withServerPart :: Name -> ExpQ
withServerPart name = appE (appE [| withServerPart_ |] (liftName name)) (varE name)
withServerPart_ :: (MonadIO m, ServerMonad m, FilterMonad Response m, WebMonad Response m) =>
Name
-> a
-> PluginHandle
-> (a -> m b)
-> m b
withServerPart_ name _fun ph use =
do r <- liftIO $ funcTH ph name
case r of
(Left e) -> escape $ internalServerError (toResponse (unlines e))
(Right f) -> use f