{-|
Module      : Gargantext.API.Dev
Description : 
Copyright   : (c) CNRS, 2017-Present
License     : AGPL + CECILL v3
Maintainer  : team@gargantext.org
Stability   : experimental
Portability : POSIX

-}

-- Use only for dev/repl
module Gargantext.API.Dev where

import Control.Exception (finally)
import Control.Monad (fail)
import Control.Monad.Reader (runReaderT)
import Gargantext.API.Admin.EnvTypes
import Gargantext.API.Admin.Settings
import Gargantext.API.Ngrams (saveNodeStory)
import Gargantext.API.Prelude
import Gargantext.Core.NodeStory
import Gargantext.Database.Prelude
import Gargantext.Prelude
import Gargantext.Prelude.Config (GargConfig(..), readConfig)
import qualified Gargantext.Prelude.Mail as Mail
import Servant
import System.IO (FilePath)

type IniPath  = FilePath
-------------------------------------------------------------------
withDevEnv :: IniPath -> (DevEnv -> IO a) -> IO a
withDevEnv :: IniPath -> (DevEnv -> IO a) -> IO a
withDevEnv IniPath
iniPath DevEnv -> IO a
k = do
  DevEnv
env <- IO DevEnv
newDevEnv
  DevEnv -> IO a
k DevEnv
env -- `finally` cleanEnv env

  where
    newDevEnv :: IO DevEnv
newDevEnv = do
      GargConfig
cfg     <- IniPath -> IO GargConfig
readConfig         IniPath
iniPath
      ConnectInfo
dbParam <- IniPath -> IO ConnectInfo
databaseParameters IniPath
iniPath
      NodeStoryEnv
nodeStory_env <- IniPath -> IO NodeStoryEnv
readNodeStoryEnv (GargConfig -> IniPath
_gc_repofilepath GargConfig
cfg)
      Pool Connection
pool    <- ConnectInfo -> IO (Pool Connection)
newPool            ConnectInfo
dbParam
      Settings
setts   <- IniPath -> IO Settings
devSettings IniPath
devJwkFile
      MailConfig
mail    <- IniPath -> IO MailConfig
Mail.readConfig IniPath
iniPath
      DevEnv -> IO DevEnv
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DevEnv -> IO DevEnv) -> DevEnv -> IO DevEnv
forall a b. (a -> b) -> a -> b
$ DevEnv :: Settings
-> GargConfig
-> Pool Connection
-> NodeStoryEnv
-> MailConfig
-> DevEnv
DevEnv
        { _dev_env_pool :: Pool Connection
_dev_env_pool     = Pool Connection
pool
        , _dev_env_nodeStory :: NodeStoryEnv
_dev_env_nodeStory  = NodeStoryEnv
nodeStory_env
        , _dev_env_settings :: Settings
_dev_env_settings = Settings
setts
        , _dev_env_config :: GargConfig
_dev_env_config   = GargConfig
cfg
        , _dev_env_mail :: MailConfig
_dev_env_mail      = MailConfig
mail
        }

-- | Run Cmd Sugar for the Repl (GHCI)
runCmdRepl :: Show err => Cmd'' DevEnv err a -> IO a
runCmdRepl :: Cmd'' DevEnv err a -> IO a
runCmdRepl Cmd'' DevEnv err a
f = IniPath -> (DevEnv -> IO a) -> IO a
forall a. IniPath -> (DevEnv -> IO a) -> IO a
withDevEnv IniPath
"gargantext.ini" ((DevEnv -> IO a) -> IO a) -> (DevEnv -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \DevEnv
env -> DevEnv -> Cmd'' DevEnv err a -> IO a
forall err a. Show err => DevEnv -> Cmd'' DevEnv err a -> IO a
runCmdDev DevEnv
env Cmd'' DevEnv err a
f

runCmdReplServantErr :: Cmd'' DevEnv ServerError a -> IO a
runCmdReplServantErr :: Cmd'' DevEnv ServerError a -> IO a
runCmdReplServantErr = Cmd'' DevEnv ServerError a -> IO a
forall err a. Show err => Cmd'' DevEnv err a -> IO a
runCmdRepl

-- In particular this writes the repo file after running
-- the command.
-- This function is constrained to the DevEnv rather than
-- using HasConnectionPool and HasRepoVar.
runCmdDev :: (Show err) => DevEnv -> Cmd'' DevEnv err a -> IO a
runCmdDev :: DevEnv -> Cmd'' DevEnv err a -> IO a
runCmdDev DevEnv
env Cmd'' DevEnv err a
f =
  ((err -> IO a) -> (a -> IO a) -> Either err a -> IO a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (IniPath -> IO a
forall (m :: * -> *) a. MonadFail m => IniPath -> m a
fail (IniPath -> IO a) -> (err -> IniPath) -> err -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. err -> IniPath
forall a. Show a => a -> IniPath
show) a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either err a -> IO a) -> IO (Either err a) -> IO a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< DevEnv -> Cmd'' DevEnv err a -> IO (Either err a)
forall env err a.
HasConnectionPool env =>
env -> Cmd'' env err a -> IO (Either err a)
runCmd DevEnv
env Cmd'' DevEnv err a
f)
    IO a -> IO () -> IO a
forall a b. IO a -> IO b -> IO a
`finally`
  ReaderT DevEnv IO () -> DevEnv -> IO ()
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT DevEnv IO ()
forall env (m :: * -> *).
(MonadReader env m, MonadBase IO m, HasNodeStorySaver env) =>
m ()
saveNodeStory DevEnv
env

runCmdDevNoErr :: DevEnv -> Cmd' DevEnv () a -> IO a
runCmdDevNoErr :: DevEnv -> Cmd' DevEnv () a -> IO a
runCmdDevNoErr = DevEnv -> Cmd' DevEnv () a -> IO a
forall err a. Show err => DevEnv -> Cmd'' DevEnv err a -> IO a
runCmdDev

runCmdDevServantErr :: DevEnv -> Cmd' DevEnv ServerError a -> IO a
runCmdDevServantErr :: DevEnv -> Cmd' DevEnv ServerError a -> IO a
runCmdDevServantErr = DevEnv -> Cmd' DevEnv ServerError a -> IO a
forall err a. Show err => DevEnv -> Cmd'' DevEnv err a -> IO a
runCmdDev

runCmdReplEasy :: Cmd'' DevEnv GargError a -> IO a
runCmdReplEasy :: Cmd'' DevEnv GargError a -> IO a
runCmdReplEasy Cmd'' DevEnv GargError a
f = IniPath -> (DevEnv -> IO a) -> IO a
forall a. IniPath -> (DevEnv -> IO a) -> IO a
withDevEnv IniPath
"gargantext.ini" ((DevEnv -> IO a) -> IO a) -> (DevEnv -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \DevEnv
env -> DevEnv -> Cmd'' DevEnv GargError a -> IO a
forall err a. Show err => DevEnv -> Cmd'' DevEnv err a -> IO a
runCmdDev DevEnv
env Cmd'' DevEnv GargError a
f