More explicit error handling and messages when required environment variables are missing
This commit is contained in:
parent
5072fb4df4
commit
66cf9d4600
@ -1,7 +1,11 @@
|
|||||||
module Core.Configuration where
|
module Core.Configuration ( adminEmail, appPort
|
||||||
|
, confLinkLength, dataPath, dbPath
|
||||||
|
, encKey, getRuntimeEnvironment
|
||||||
|
, keyFileInit, main) where
|
||||||
|
|
||||||
import qualified Data.ByteString as B
|
import qualified Data.ByteString as B
|
||||||
|
|
||||||
|
import Control.Monad (mapM)
|
||||||
import Core.Types
|
import Core.Types
|
||||||
import Crypto.Saltine.Core.SecretBox (newKey)
|
import Crypto.Saltine.Core.SecretBox (newKey)
|
||||||
import Crypto.Saltine.Class (encode)
|
import Crypto.Saltine.Class (encode)
|
||||||
@ -9,13 +13,35 @@ import Configuration.Dotenv
|
|||||||
import System.Directory (doesFileExist)
|
import System.Directory (doesFileExist)
|
||||||
import System.Environment (getEnv, lookupEnv)
|
import System.Environment (getEnv, lookupEnv)
|
||||||
|
|
||||||
-- Make the dotenv file configuration available if PURRNOFILE is not present
|
-- Load environment variables from dotenv file if required
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
envFile <- lookupEnv "PURRNOFILE"
|
reqEnvLookup <- getRequiredEnv
|
||||||
case envFile of
|
if (Nothing `elem` reqEnvLookup)
|
||||||
Nothing -> loadFile defaultConfig
|
then checkEnvFile requiredEnvVars
|
||||||
_ -> putStrLn "Not using dotenv file"
|
else pure ()
|
||||||
|
where
|
||||||
|
getRequiredEnv :: IO [Maybe String]
|
||||||
|
getRequiredEnv = mapM (\s -> lookupEnv s) requiredEnvVars
|
||||||
|
|
||||||
|
checkEnvFile :: [String] -> IO ()
|
||||||
|
checkEnvFile requiredEnv = do
|
||||||
|
dotEnvExists <- doesFileExist "./.env"
|
||||||
|
if dotEnvExists
|
||||||
|
then do
|
||||||
|
loadFile defaultConfig
|
||||||
|
fromEnvFile <- getRequiredEnv
|
||||||
|
if (Nothing `elem` fromEnvFile)
|
||||||
|
then error $ missingEnvMsg requiredEnv
|
||||||
|
else pure ()
|
||||||
|
else error $ "Cannot find .env file in application directory.\n"
|
||||||
|
++ missingEnvMsg requiredEnv
|
||||||
|
|
||||||
|
missingEnvMsg :: [String] -> String
|
||||||
|
missingEnvMsg required =
|
||||||
|
"Missing required environment variable(s).\n"
|
||||||
|
++ "All required environment variables:\n"
|
||||||
|
++ unlines required
|
||||||
|
|
||||||
-- Check if an encryption key exists on the filesystem and create one if not
|
-- Check if an encryption key exists on the filesystem and create one if not
|
||||||
keyFileInit :: IO ()
|
keyFileInit :: IO ()
|
||||||
@ -53,3 +79,8 @@ dbPath = "data/Purr.sqlite"
|
|||||||
|
|
||||||
confLinkLength :: IO String
|
confLinkLength :: IO String
|
||||||
confLinkLength = getEnv "LINKLENGTH"
|
confLinkLength = getEnv "LINKLENGTH"
|
||||||
|
|
||||||
|
requiredEnvVars :: [String]
|
||||||
|
requiredEnvVars = [ "ADMINEMAIL", "APPLICATIONHOST", "APPLICATIONPORT"
|
||||||
|
, "DATADIR", "ENVIRONMENT", "LINKLENGTH"
|
||||||
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user