2 Module : Gargantext.API.Dev
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 -- Use only for dev/repl
13 module Gargantext.API.Dev where
15 import Control.Exception (finally)
16 import Control.Monad (fail)
17 import Control.Monad.Reader (runReaderT)
18 import Gargantext.API.Admin.EnvTypes
19 import Gargantext.API.Admin.Settings
20 import Gargantext.API.Ngrams (saveRepo)
21 import Gargantext.API.Prelude
22 import Gargantext.Core.NodeStory
23 import Gargantext.Database.Prelude
24 import Gargantext.Prelude
25 import Gargantext.Prelude.Config (GargConfig(..), readConfig)
27 import System.IO (FilePath)
29 type IniPath = FilePath
30 -------------------------------------------------------------------
31 withDevEnv :: IniPath -> (DevEnv -> IO a) -> IO a
32 withDevEnv iniPath k = do
35 -- k env `finally` cleanEnv env
39 cfg <- readConfig iniPath
40 dbParam <- databaseParameters iniPath
41 nodeStory_env <- readNodeStoryEnv (_gc_repofilepath cfg)
42 pool <- newPool dbParam
43 setts <- devSettings devJwkFile
45 { _dev_env_pool = pool
46 , _dev_env_nodeStory = nodeStory_env
47 , _dev_env_settings = setts
48 , _dev_env_config = cfg
51 -- | Run Cmd Sugar for the Repl (GHCI)
52 runCmdRepl :: Show err => Cmd'' DevEnv err a -> IO a
53 runCmdRepl f = withDevEnv "gargantext.ini" $ \env -> runCmdDev env f
55 runCmdReplServantErr :: Cmd'' DevEnv ServerError a -> IO a
56 runCmdReplServantErr = runCmdRepl
58 -- In particular this writes the repo file after running
60 -- This function is constrained to the DevEnv rather than
61 -- using HasConnectionPool and HasRepoVar.
62 runCmdDev :: (Show err) => DevEnv -> Cmd'' DevEnv err a -> IO a
64 (either (fail . show) pure =<< runCmd env f)
66 runReaderT saveRepo env
68 runCmdDevNoErr :: DevEnv -> Cmd' DevEnv () a -> IO a
69 runCmdDevNoErr = runCmdDev
71 runCmdDevServantErr :: DevEnv -> Cmd' DevEnv ServerError a -> IO a
72 runCmdDevServantErr = runCmdDev
74 runCmdReplEasy :: Cmd'' DevEnv GargError a -> IO a
75 runCmdReplEasy f = withDevEnv "gargantext.ini" $ \env -> runCmdDev env f