]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Admin/Types.hs
Cleanup refactoring of config/settings/env
[gargantext.git] / src / Gargantext / API / Admin / Types.hs
1 -- |
2
3 {-# LANGUAGE TemplateHaskell #-}
4
5 module Gargantext.API.Admin.Types where
6
7 import Control.Lens
8 import Control.Monad.Logger
9 import Data.ByteString (ByteString)
10 import Data.Pool (Pool)
11 import Database.PostgreSQL.Simple (Connection)
12 import GHC.Enum
13 import GHC.Generics (Generic)
14 import Network.HTTP.Client (Manager)
15 import Servant.Auth.Server (JWTSettings, CookieSettings(..))
16 import Servant.Client (BaseUrl)
17 import Servant.Job.Async (HasJobEnv(..), Job)
18 import System.Log.FastLogger
19 import qualified Servant.Job.Core
20
21 import Gargantext.API.Admin.Orchestrator.Types
22 import Gargantext.API.Ngrams.Types (HasRepoVar(..), HasRepoSaver(..), HasRepo(..), RepoEnv(..))
23 import Gargantext.Database.Prelude (HasConnectionPool(..), HasConfig(..))
24 import Gargantext.Prelude
25 import Gargantext.Prelude.Config (GargConfig(..))
26
27 type PortNumber = Int
28
29 data SendEmailType = SendEmailViaAws
30 | LogEmailToConsole
31 | WriteEmailToFile
32 deriving (Show, Read, Enum, Bounded, Generic)
33
34
35 data Settings = Settings
36 { _allowedOrigin :: ByteString -- allowed origin for CORS
37 , _allowedHost :: ByteString -- allowed host for CORS
38 , _appPort :: PortNumber
39 , _logLevelLimit :: LogLevel -- log level from the monad-logger package
40 -- , _dbServer :: Text
41 -- ^ this is not used yet
42 , _jwtSettings :: JWTSettings
43 , _cookieSettings :: CookieSettings
44 , _sendLoginEmails :: SendEmailType
45 , _scrapydUrl :: BaseUrl
46 }
47
48 makeLenses ''Settings
49
50 class HasSettings env where
51 settings :: Getter env Settings
52
53
54 data FireWall = FireWall { unFireWall :: Bool }
55
56 data Env = Env
57 { _env_settings :: !Settings
58 , _env_logger :: !LoggerSet
59 , _env_pool :: !(Pool Connection)
60 , _env_repo :: !RepoEnv
61 , _env_manager :: !Manager
62 , _env_self_url :: !BaseUrl
63 , _env_scrapers :: !ScrapersEnv
64 , _env_config :: !GargConfig
65 }
66 deriving (Generic)
67
68 makeLenses ''Env
69
70 instance HasConfig Env where
71 config = env_config
72
73 instance HasConnectionPool Env where
74 connPool = env_pool
75
76 instance HasRepoVar Env where
77 repoVar = repoEnv . repoVar
78
79 instance HasRepoSaver Env where
80 repoSaver = repoEnv . repoSaver
81
82 instance HasRepo Env where
83 repoEnv = env_repo
84
85 instance HasSettings Env where
86 settings = env_settings
87
88 instance Servant.Job.Core.HasEnv Env (Job JobLog JobLog) where
89 _env = env_scrapers . Servant.Job.Core._env
90
91 instance HasJobEnv Env JobLog JobLog where
92 job_env = env_scrapers
93
94 data MockEnv = MockEnv
95 { _menv_firewall :: !FireWall
96 }
97 deriving (Generic)
98
99 makeLenses ''MockEnv
100
101
102 data DevEnv = DevEnv
103 { _dev_env_pool :: !(Pool Connection)
104 , _dev_env_repo :: !RepoEnv
105 , _dev_env_settings :: !Settings
106 , _dev_env_config :: !GargConfig
107 }
108
109 makeLenses ''DevEnv
110
111 instance HasConfig DevEnv where
112 config = dev_env_config
113
114 instance HasConnectionPool DevEnv where
115 connPool = dev_env_pool
116
117 instance HasRepoVar DevEnv where
118 repoVar = repoEnv . repoVar
119
120 instance HasRepoSaver DevEnv where
121 repoSaver = repoEnv . repoSaver
122
123 instance HasRepo DevEnv where
124 repoEnv = dev_env_repo
125
126 instance HasSettings DevEnv where
127 settings = dev_env_settings