7 Database/NodeNodeNgram.hs
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
21 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
23 {-# LANGUAGE NoImplicitPrelude #-}
24 {-# LANGUAGE DataKinds #-}
25 {-# LANGUAGE DeriveGeneric #-}
26 {-# LANGUAGE ScopedTypeVariables #-}
27 {-# LANGUAGE TemplateHaskell #-}
28 {-# LANGUAGE OverloadedStrings #-}
29 {-# LANGUAGE FlexibleInstances #-}
31 module Gargantext.API.Settings
34 import System.Log.FastLogger
36 import GHC.Generics (Generic)
37 import Prelude (Bounded())
38 import System.Environment (lookupEnv)
40 -- import Control.Applicative ((<*>))
42 import Data.Maybe (fromMaybe)
43 import Data.Either (either)
46 import Data.Text.Encoding (encodeUtf8)
47 import Data.ByteString.Lazy.Internal
50 import Web.HttpApiData (parseUrlPiece)
51 import qualified Jose.Jwk as Jose
52 import qualified Jose.Jwa as Jose
54 import Control.Monad.Logger
56 import Gargantext.Prelude
59 data SendEmailType = SendEmailViaAws
62 deriving (Show, Read, Enum, Bounded, Generic)
65 data Settings = Settings
66 { _allowedOrigin :: ByteString -- ^ allowed origin for CORS
67 , _allowedHost :: ByteString -- ^ allowed host for CORS
69 , _logLevelLimit :: LogLevel -- ^ log level from the monad-logger package
71 , _jwtSecret :: Jose.Jwk -- ^ key from the jose-jwt package
72 , _sendLoginEmails :: SendEmailType
78 parseJwk :: Text -> Jose.Jwk
79 parseJwk secretStr = jwk
81 secretBs = encodeUtf8 secretStr
82 jwk = Jose.SymmetricJwk secretBs
85 (Just $ Jose.Signed Jose.HS256)
87 devSettings :: Settings
88 devSettings = Settings
89 { _allowedOrigin = "http://localhost:8008"
90 , _allowedHost = "localhost: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
103 reqSetting :: FromHttpApiData a => Text -> IO a
105 e <- fromMaybe (panic $ "Missing " <> name) <$> lookupEnv (unpack name)
106 pure $ either (panic $ "Unable to parse " <> name) identity $ parseUrlPiece $ pack e
108 optSetting :: FromHttpApiData a => Text -> a -> IO a
109 optSetting name d = do
110 me <- lookupEnv (unpack name)
113 Just e -> pure $ either (panic $ "Unable to parse " <> name) identity $ parseUrlPiece $ pack e
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
128 { _settings :: Settings
129 , _logger :: LoggerSet
130 -- , _dbConfig :: ConnectionPool -- from Database.Persist.Postgresql
134 createEnv :: Settings -> IO Env
135 createEnv _ = undefined {- implementation here: connect to db, init logger, etc -}