]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Settings.hs
[FIX] double -> int for hyper data dates info.
[gargantext.git] / src / Gargantext / API / Settings.hs
1 {-|
2 Module : Gargantext.API.Settings
3 Description : Settings of the API (Server and Client)
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 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
13
14 {-# LANGUAGE DataKinds #-}
15 {-# LANGUAGE DeriveGeneric #-}
16 {-# LANGUAGE ScopedTypeVariables #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE OverloadedStrings #-}
19 {-# LANGUAGE FlexibleInstances #-}
20
21 module Gargantext.API.Settings
22 where
23
24 import System.Log.FastLogger
25 import GHC.Enum
26 import GHC.Generics (Generic)
27 import Prelude (Bounded())
28 import System.Environment (lookupEnv)
29
30 -- import Control.Applicative ((<*>))
31
32 import Data.Maybe (fromMaybe)
33 import Data.Either (either)
34 -- import Data.Map
35 import Data.Text
36 import Data.Text.Encoding (encodeUtf8)
37 import Data.ByteString.Lazy.Internal
38
39 import Servant
40 import Web.HttpApiData (parseUrlPiece)
41 import qualified Jose.Jwk as Jose
42 import qualified Jose.Jwa as Jose
43
44 import Control.Monad.Logger
45 import Control.Lens
46 import Gargantext.Prelude
47
48
49 data SendEmailType = SendEmailViaAws
50 | LogEmailToConsole
51 | WriteEmailToFile
52 deriving (Show, Read, Enum, Bounded, Generic)
53
54
55 data Settings = Settings
56 { _allowedOrigin :: ByteString -- ^ allowed origin for CORS
57 , _allowedHost :: ByteString -- ^ allowed host for CORS
58 , _appPort :: Int
59 , _logLevelLimit :: LogLevel -- ^ log level from the monad-logger package
60 , _dbServer :: Text
61 , _jwtSecret :: Jose.Jwk -- ^ key from the jose-jwt package
62 , _sendLoginEmails :: SendEmailType
63 }
64
65 makeLenses ''Settings
66
67
68 parseJwk :: Text -> Jose.Jwk
69 parseJwk secretStr = jwk
70 where
71 secretBs = encodeUtf8 secretStr
72 jwk = Jose.SymmetricJwk secretBs
73 Nothing
74 Nothing
75 (Just $ Jose.Signed Jose.HS256)
76
77 devSettings :: Settings
78 devSettings = Settings
79 { _allowedOrigin = "http://localhost:8008"
80 , _allowedHost = "localhost:3000"
81 , _appPort = 3000
82 , _logLevelLimit = LevelDebug
83 , _dbServer = "localhost"
84 -- generate with dd if=/dev/urandom bs=1 count=32 | base64
85 -- make sure jwtSecret differs between development and production, because you do not want
86 -- your production key inside source control.
87 , _jwtSecret = parseJwk "MVg0YAPVSPiYQc/qIs/rV/X32EFR0zOJWfHFgMbszMw="
88 , _sendLoginEmails = LogEmailToConsole
89 }
90
91
92
93 reqSetting :: FromHttpApiData a => Text -> IO a
94 reqSetting name = do
95 e <- fromMaybe (panic $ "Missing " <> name) <$> lookupEnv (unpack name)
96 pure $ either (panic $ "Unable to parse " <> name) identity $ parseUrlPiece $ pack e
97
98 optSetting :: FromHttpApiData a => Text -> a -> IO a
99 optSetting name d = do
100 me <- lookupEnv (unpack name)
101 case me of
102 Nothing -> pure d
103 Just e -> pure $ either (panic $ "Unable to parse " <> name) identity $ parseUrlPiece $ pack e
104
105 --settingsFromEnvironment :: IO Settings
106 --settingsFromEnvironment =
107 -- Settings <$> (encodeUtf8 <$> reqSetting "ALLOWED_ORIGIN")
108 -- <*> (encodeUtf8 <$> reqSetting "ALLOWED_HOST")
109 -- <*> optSetting "PORT" 3000
110 -- <*> (parseLogLevel <$> optSetting "LOG_LEVEL" "warn")
111 -- <*> reqSetting "DB_SERVER"
112 -- <*> (parseJwk <$> reqSetting "JWT_SECRET")
113 -- <*> optSetting "SEND_EMAIL" SendEmailViaAws
114
115
116
117 data Env = Env
118 { _settings :: Settings
119 , _logger :: LoggerSet
120 -- , _dbConfig :: ConnectionPool -- from Database.Persist.Postgresql
121 }
122
123 makeLenses ''Env
124 createEnv :: Settings -> IO Env
125 createEnv _ = undefined {- implementation here: connect to db, init logger, etc -}