Copyright | 2019 Daniel YU |
---|---|
License | BSD3 |
Maintainer | leptonyu@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Boot application by using plugins.
- Motivation
Simplify to create an application in Haskell.
When we decide to create an application using Haskell.
We may need using configurations, loggers as basic functions.
If this application needs storages, caches, etc.,
then we have to weaving the management of connection of these facilities into the application.
Connections need to be created before and be destroyed after using them.
There is a common strategy to manage connections, that is using Cont
.
Then we can encapsulate the management of connections separately.
For example, we can write a database plugin Plugin
cxt
m
DBConnection
,
which can manage the database connections in monad m
with context cxt
.
Context cxt
may be requested for getting configurations or logging functions.
When all the components of application are encapsulated by plugins, then building an application will be simplified.
- Have a Try
>>>
booting (pluginSimple "application") (logInfo "hello")
2019-07-27 19:35:30 INFO [application] Ghci1 - hello>>>
booting (pluginSimple "application") (require "user" >>= logInfo)
2019-07-27 19:37:45 INFO [application] Ghci2 - daniel
Main
main :: IO () main = bootApp (pluginSimple "application") go where go = forever $ do user <- require "user" -- Request for configuration. logInfo $ "Hello, " <> user <> "!" -- Request for logging. liftIO $ threadDelay 1000000
Synopsis
- boot :: Monad m => Plugin () m (m ()) -> m ()
- data Plugin i m u
- runPlugin :: i -> Plugin i m u -> (u -> m ()) -> m ()
- promote :: i -> Plugin i m u -> Plugin x m u
- combine :: [Plugin i m i] -> Plugin i m i
- withPlugin :: (i -> j) -> Plugin j m u -> Plugin i m u
- mapPlugin :: (m () -> m ()) -> Plugin i m u -> Plugin i m u
- isoPlugin :: (m () -> n ()) -> (n () -> m ()) -> Plugin i n u -> Plugin i m u
- bracketP :: forall m i u. MonadCatch m => m u -> (u -> m ()) -> Plugin i m u
- wrapP :: ((u -> m ()) -> m ()) -> Plugin i m u
- bootApp :: Plugin () m cxt -> AppT cxt m () -> m ()
- data AppT cxt m a
- type App cxt = AppT cxt IO
- runAppT :: cxt -> AppT cxt m a -> m a
- liftIO :: MonadIO m => IO a -> m a
- ask :: MonadReader r m => m r
- lift :: (MonadTrans t, Monad m) => m a -> t m a
- throwM :: (MonadThrow m, Exception e) => e -> m a
- module Boots.Plugin.Simple
Plugin
Plugin generates component u
with the context of component i
running in monad m
.
Instances
MonadReader i (Plugin i m) Source # | |
MonadTrans (Plugin i) Source # | |
Defined in Boots.Internal.Plugin | |
Monad (Plugin i m) Source # | |
Functor (Plugin i m) Source # | |
Applicative (Plugin i m) Source # | |
Defined in Boots.Internal.Plugin | |
MonadIO m => MonadIO (Plugin i m) Source # | |
Defined in Boots.Internal.Plugin | |
MonadThrow m => MonadThrow (Plugin i m) Source # | |
Defined in Boots.Internal.Plugin | |
(MonadIO m, HasLogger cxt) => MonadLogger (Plugin cxt m) Source # | |
Defined in Boots.Plugin.Logger | |
(MonadIO m, HasLogger cxt) => MonadLoggerIO (Plugin cxt m) Source # | |
Defined in Boots.Plugin.Logger | |
Monad m => MonadCont (Plugin i m) Source # | |
HasSalak cxt => MonadSalak (Plugin cxt m) Source # | |
Defined in Boots.Plugin.Salak |
withPlugin :: (i -> j) -> Plugin j m u -> Plugin i m u Source #
Convert a plugin into another.
mapPlugin :: (m () -> m ()) -> Plugin i m u -> Plugin i m u Source #
Apply a function to transform the result of a continuation-passing computation.
isoPlugin :: (m () -> n ()) -> (n () -> m ()) -> Plugin i n u -> Plugin i m u Source #
Transform a plugin with monad n
to a plugin with monad m
.
:: MonadCatch m | |
=> m u | Open resource. |
-> (u -> m ()) | Close resource. |
-> Plugin i m u | Resource plugin. |
Create bracket style plugin, used for manage resources, which need to open and close.
A simple example:
>>>
res = bracketP (putStrLn "open") (const $ putStrLn "close")
>>>
runPlugin () res (const $ putStrLn "using")
open using close
Application
Application monad transformation.
Instances
Monad m => MonadReader cxt (AppT cxt m) Source # | |
MonadTrans (AppT cxt) Source # | |
Defined in Boots.Internal.App | |
Monad m => Monad (AppT cxt m) Source # | |
Functor m => Functor (AppT cxt m) Source # | |
Applicative m => Applicative (AppT cxt m) Source # | |
Defined in Boots.Internal.App | |
MonadIO m => MonadIO (AppT cxt m) Source # | |
Defined in Boots.Internal.App | |
MonadUnliftIO m => MonadUnliftIO (AppT cxt m) Source # | |
Defined in Boots.Internal.App | |
MonadThrow m => MonadThrow (AppT cxt m) Source # | |
Defined in Boots.Internal.App | |
MonadCatch m => MonadCatch (AppT cxt m) Source # | |
MonadMask m => MonadMask (AppT cxt m) Source # | |
Defined in Boots.Internal.App | |
(MonadIO m, HasLogger cxt) => MonadLogger (AppT cxt m) Source # | |
Defined in Boots.Plugin.Logger | |
(MonadIO m, HasLogger cxt) => MonadLoggerIO (AppT cxt m) Source # | |
Defined in Boots.Plugin.Logger | |
(Monad m, HasSalak cxt) => MonadSalak (AppT cxt m) Source # | |
Defined in Boots.Plugin.Salak |
ask :: MonadReader r m => m r #
Retrieves the monad environment.
lift :: (MonadTrans t, Monad m) => m a -> t m a #
Lift a computation from the argument monad to the constructed monad.
throwM :: (MonadThrow m, Exception e) => e -> m a #
Throw an exception. Note that this throws when this action is run in
the monad m
, not when it is applied. It is a generalization of
Control.Exception's throwIO
.
Should satisfy the law:
throwM e >> f = throwM e
module Boots.Plugin.Simple