]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Prelude/Config.hs
Merge branch 'dev-distributional' of ssh://gitlab.iscpif.fr:20022/gargantext/haskell...
[gargantext.git] / src / Gargantext / Prelude / Config.hs
1 {-|
2 Module : Gargantext.Prelude.Config
3 Description : Textmining Collaborative Platform
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 {-# LANGUAGE TemplateHaskell #-}
13
14 module Gargantext.Prelude.Config where
15
16 import Prelude (read)
17 import System.IO (FilePath)
18 import Data.Ini (readIniFile, lookupValue)
19 import Data.Either.Extra (Either(Left, Right))
20 import Data.Text as T
21 import GHC.Generics (Generic)
22 import Control.Lens (makeLenses)
23
24 import Gargantext.Prelude
25
26
27 -- | strip a given character from end of string
28 stripRight :: Char -> T.Text -> T.Text
29 stripRight c s = if T.last s == c then stripRight c (T.take (T.length s - 1) s) else s
30
31 data GargConfig = GargConfig { _gc_url :: !T.Text
32 , _gc_url_backend_api :: !T.Text
33
34 , _gc_masteruser :: !T.Text
35 , _gc_secretkey :: !T.Text
36
37 , _gc_datafilepath :: !FilePath
38 , _gc_repofilepath :: !FilePath
39
40 , _gc_frame_write_url :: !T.Text
41 , _gc_frame_calc_url :: !T.Text
42
43 , _gc_frame_searx_url :: !T.Text
44 , _gc_frame_istex_url :: !T.Text
45
46 , _gc_max_docs_scrapers :: !Integer
47 }
48 deriving (Generic, Show)
49
50 makeLenses ''GargConfig
51
52 readConfig :: FilePath -> IO GargConfig
53 readConfig fp = do
54 ini <- readIniFile fp
55 let ini'' = case ini of
56 Left e -> panic (T.pack $ "gargantext.ini not found" <> show e)
57 Right ini' -> ini'
58
59 let val x = case (lookupValue (T.pack "gargantext") (T.pack x) ini'') of
60 Left _ -> panic (T.pack $ "ERROR: add " <> x <> " to your gargantext.ini")
61 Right p' -> p'
62
63 pure $ GargConfig (stripRight '/' $ val "URL")
64 (stripRight '/' $ val "URL_BACKEND_API")
65 (val "MASTER_USER")
66 (val "SECRET_KEY")
67 (cs $ val "DATA_FILEPATH")
68 (cs $ val "REPO_FILEPATH")
69 (stripRight '/' $ val "FRAME_WRITE_URL")
70 (stripRight '/' $ val "FRAME_CALC_URL")
71 (stripRight '/' $ val "FRAME_SEARX_URL")
72 (stripRight '/' $ val "FRAME_ISTEX_URL")
73 (read $ cs $ val "MAX_DOCS_SCRAPERS")
74
75 {- UNUSED
76 defaultConfig :: GargConfig
77 defaultConfig = GargConfig "https://localhost"
78 "https://localhost:8008/api/v1.0"
79 "gargantua"
80 "secret"
81 "data"
82 "repos/"
83 "https://frame_write.url"
84 "https://frame_calc.url"
85 "https://frame_searx.url"
86 "https://frame_istex.url"
87 1000
88 -}