]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Dev.hs
Merge branch 'dev' into dev-hackathon-fixes
[gargantext.git] / src / Gargantext / API / Dev.hs
1 {-|
2 Module : Gargantext.API.Dev
3 Description :
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -}
11
12 -- Use only for dev/repl
13 module Gargantext.API.Dev where
14
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 (saveNodeStoryImmediate)
21 import Gargantext.API.Prelude
22 import Gargantext.Core.NodeStory
23 import Gargantext.Database.Prelude
24 import Gargantext.Prelude
25 import Gargantext.Prelude.Config (readConfig)
26 import qualified Gargantext.Prelude.Mail as Mail
27 import Servant
28 import System.IO (FilePath)
29
30 type IniPath = FilePath
31 -------------------------------------------------------------------
32 withDevEnv :: IniPath -> (DevEnv -> IO a) -> IO a
33 withDevEnv iniPath k = do
34 env <- newDevEnv
35 k env -- `finally` cleanEnv env
36
37 where
38 newDevEnv = do
39 cfg <- readConfig iniPath
40 dbParam <- databaseParameters iniPath
41 --nodeStory_env <- readNodeStoryEnv (_gc_repofilepath cfg)
42 pool <- newPool dbParam
43 nodeStory_env <- readNodeStoryEnv pool
44 setts <- devSettings devJwkFile
45 mail <- Mail.readConfig iniPath
46 pure $ DevEnv
47 { _dev_env_pool = pool
48 , _dev_env_nodeStory = nodeStory_env
49 , _dev_env_settings = setts
50 , _dev_env_config = cfg
51 , _dev_env_mail = mail
52 }
53
54 -- | Run Cmd Sugar for the Repl (GHCI)
55 runCmdRepl :: Show err => Cmd'' DevEnv err a -> IO a
56 runCmdRepl f = withDevEnv "gargantext.ini" $ \env -> runCmdDev env f
57
58 runCmdReplServantErr :: Cmd'' DevEnv ServerError a -> IO a
59 runCmdReplServantErr = runCmdRepl
60
61 -- In particular this writes the repo file after running
62 -- the command.
63 -- This function is constrained to the DevEnv rather than
64 -- using HasConnectionPool and HasRepoVar.
65 runCmdDev :: Show err => DevEnv -> Cmd'' DevEnv err a -> IO a
66 runCmdDev env f =
67 (either (fail . show) pure =<< runCmd env f)
68 `finally`
69 runReaderT saveNodeStoryImmediate env
70
71 runCmdDevNoErr :: DevEnv -> Cmd' DevEnv () a -> IO a
72 runCmdDevNoErr = runCmdDev
73
74 runCmdDevServantErr :: DevEnv -> Cmd' DevEnv ServerError a -> IO a
75 runCmdDevServantErr = runCmdDev
76
77 runCmdReplEasy :: Cmd'' DevEnv GargError a -> IO a
78 runCmdReplEasy f = withDevEnv "gargantext.ini" $ \env -> runCmdDev env f