aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.hs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2019-01-23 00:02:42 +0100
committertv <tv@krebsco.de>2019-01-23 00:57:36 +0100
commitd40815fd56bf1895af89b72b1171675a2e0ae5f7 (patch)
tree83b96a701f16b13915836c3a6c94463732a9f6d8 /src/main.hs
parenta00da57346c195b1b15d1c6aca2891483901aae6 (diff)
src: use more simple functionsv0.1.0
Diffstat (limited to 'src/main.hs')
-rw-r--r--src/main.hs46
1 files changed, 42 insertions, 4 deletions
diff --git a/src/main.hs b/src/main.hs
index db5e54a..89966c2 100644
--- a/src/main.hs
+++ b/src/main.hs
@@ -1,14 +1,52 @@
{-# LANGUAGE LambdaCase #-}
+{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
-import Data.Aeson (eitherDecodeFileStrict)
+import Control.Lens
+import Data.Aeson
+import Data.Aeson (Value)
+import Data.Aeson.Lens
+import Data.Aeson.Types
+import Data.Text (Text)
+import Prelude.Extended
import qualified Reaktor
+import qualified Reaktor.Plugins.Mention
+import qualified Reaktor.Plugins.Ping
+import qualified Reaktor.Plugins.Register
+import qualified Reaktor.Plugins.System
import qualified System.Environment
main :: IO ()
main = do
[configPath] <- System.Environment.getArgs
- eitherDecodeFileStrict configPath >>= \case
- Right cfg -> Reaktor.run cfg
- Left err -> error err
+
+ v <- preview _Value <$> readFile configPath
+
+ Reaktor.run (reaktorConfig v) $ \actions ->
+ mapM id [
+ Reaktor.Plugins.Mention.new actions,
+ Reaktor.Plugins.Ping.new actions,
+ Reaktor.Plugins.Register.new (pluginConfig "register" v) actions,
+ Reaktor.Plugins.System.new (pluginConfig "system" v) actions
+ ]
+
+
+reaktorConfig :: (FromJSON b, Default b) => Maybe Value -> b
+reaktorConfig = maybe def parseOrDie
+
+pluginConfig :: (AsValue a, FromJSON b, Default b) => Text -> Maybe a -> b
+pluginConfig k v = maybe def parseOrDie (v ^? plugin k)
+
+
+plugin :: (Applicative f, AsValue a) =>
+ Text -> (Value -> f Value) -> Maybe a -> f (Maybe a)
+plugin k = _Just
+ . key "plugins"
+ . values
+ . filtered (has (key "plugin" . _String . only k))
+ . key "config"
+
+
+parseOrDie :: FromJSON p => Value -> p
+parseOrDie = either error id . parseEither parseJSON