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
12 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
14 {-# LANGUAGE DataKinds #-}
15 {-# LANGUAGE DeriveGeneric #-}
16 {-# LANGUAGE ScopedTypeVariables #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE OverloadedStrings #-}
19 {-# LANGUAGE FlexibleInstances #-}
21 module Gargantext.API.Settings
24 import System.Log.FastLogger
26 import GHC.Generics (Generic)
27 import Prelude (Bounded())
28 import System.Environment (lookupEnv)
30 -- import Control.Applicative ((<*>))
32 import Data.Maybe (fromMaybe)
33 import Data.Either (either)
36 import Data.Text.Encoding (encodeUtf8)
37 import Data.ByteString.Lazy.Internal
40 import Web.HttpApiData (parseUrlPiece)
41 import qualified Jose.Jwk as Jose
42 import qualified Jose.Jwa as Jose
44 import Control.Monad.Logger
46 import Gargantext.Prelude
49 data SendEmailType = SendEmailViaAws
52 deriving (Show, Read, Enum, Bounded, Generic)
55 data Settings = Settings
56 { _allowedOrigin :: ByteString -- ^ allowed origin for CORS
57 , _allowedHost :: ByteString -- ^ allowed host for CORS
59 , _logLevelLimit :: LogLevel -- ^ log level from the monad-logger package
61 , _jwtSecret :: Jose.Jwk -- ^ key from the jose-jwt package
62 , _sendLoginEmails :: SendEmailType
68 parseJwk :: Text -> Jose.Jwk
69 parseJwk secretStr = jwk
71 secretBs = encodeUtf8 secretStr
72 jwk = Jose.SymmetricJwk secretBs
75 (Just $ Jose.Signed Jose.HS256)
77 devSettings :: Settings
78 devSettings = Settings
79 { _allowedOrigin = "http://localhost:8008"
80 , _allowedHost = "localhost: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
93 reqSetting :: FromHttpApiData a => Text -> IO a
95 e <- fromMaybe (panic $ "Missing " <> name) <$> lookupEnv (unpack name)
96 pure $ either (panic $ "Unable to parse " <> name) identity $ parseUrlPiece $ pack e
98 optSetting :: FromHttpApiData a => Text -> a -> IO a
99 optSetting name d = do
100 me <- lookupEnv (unpack name)
103 Just e -> pure $ either (panic $ "Unable to parse " <> name) identity $ parseUrlPiece $ pack e
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
118 { _settings :: Settings
119 , _logger :: LoggerSet
120 -- , _dbConfig :: ConnectionPool -- from Database.Persist.Postgresql
124 createEnv :: Settings -> IO Env
125 createEnv _ = undefined {- implementation here: connect to db, init logger, etc -}