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