diff --git a/app/Helpers/Database.hs b/app/Helpers/Database.hs index 5af8706..37e2dc9 100644 --- a/app/Helpers/Database.hs +++ b/app/Helpers/Database.hs @@ -4,7 +4,7 @@ module Helpers.Database where import Database.SQLite.Simple (close, execute, open, query, Only(Only), ToRow, Query (Query), Connection) -import Helpers.Globals (getDbPath) +import Helpers.Settings (getDbPath) import Data.List (intercalate, inits) import Data.Text (pack, Text) diff --git a/app/Helpers/Globals.hs b/app/Helpers/Globals.hs deleted file mode 100644 index 86ce393..0000000 --- a/app/Helpers/Globals.hs +++ /dev/null @@ -1,36 +0,0 @@ -module Helpers.Globals where - -import System.Environment (lookupEnv) - - -getDbPath :: IO String -getDbPath = do - result <- lookupEnv "HOMEPAGE_DB" - return $ case result of - (Just a) -> a - Nothing -> "./homepage.db3" - -getPort :: IO Int -getPort = do - result <- lookupEnv "HOMEPAGE_PORT" - return $ case result of - (Just a) -> read a :: Int - Nothing -> 8000 - -data LogLevel = Error | Warning | Info - -getLogLevel :: IO LogLevel -getLogLevel = do - result <- lookupEnv "HOMEPAGE_LOGLEVEL" - return $ case result of - (Just "info") -> Info - (Just "warning") -> Warning - (Just "error") -> Error - _ -> Error - -getCliState :: IO Bool -getCliState = do - result <- lookupEnv "HOMEPAGE_CLI" - case result of - (Just "1") -> return True - _ -> return False \ No newline at end of file diff --git a/app/Helpers/Logger.hs b/app/Helpers/Logger.hs index ec568f6..2ee99f3 100644 --- a/app/Helpers/Logger.hs +++ b/app/Helpers/Logger.hs @@ -6,7 +6,7 @@ import Data.Text (unpack) import Network.HTTP.Types (Status(statusCode)) import Helpers.Utils (unpackBS) -import Helpers.Globals (LogLevel (..), getLogLevel, getCliState) +import Helpers.Settings (LogLevel (..), getLogLevel, getCliState) import System.IO (hFlush, stdout) import Control.Monad (when) diff --git a/app/Helpers/Settings.hs b/app/Helpers/Settings.hs new file mode 100644 index 0000000..dccc2ac --- /dev/null +++ b/app/Helpers/Settings.hs @@ -0,0 +1,53 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Helpers.Settings where + +import System.Environment (lookupEnv, getArgs) +import Data.List (find) +import Data.Text (isInfixOf, pack, unpack) +import Data.List.Split (splitOn) + +argOrEnvOrDefault :: String -> String -> String -> IO String +argOrEnvOrDefault arg env def = do + args <- getArgs + case find (isInfixOf (pack arg)) [pack arg | arg <- args] of + (Just x) -> return $ splitOn "=" (unpack x) !! 1 + _ -> do + var <- lookupEnv env + return $ case var of + (Just a) -> a + _ -> def + +argOrEnvOrBool :: String -> String -> IO Bool +argOrEnvOrBool arg env = do + args <- getArgs + case find (== arg) args of + (Just _) -> return True + _ -> do + var <- lookupEnv env + return $ case var of + (Just "1") -> True + _ -> False + + +getDbPath :: IO String +getDbPath = argOrEnvOrDefault "--db" "HOMEPAGE_DB" "./homrpage.db3" + +getPort :: IO Int +getPort = do + var <- argOrEnvOrDefault "--port" "HOMEPAGE_PORT" "8000" + return $ read var + +data LogLevel = Error | Warning | Info + +getLogLevel :: IO LogLevel +getLogLevel = do + var <- argOrEnvOrDefault "--loglevel" "HOMEPAGE_LOGLEVEL" "error" + return $ case var of + "info" -> Info + "warning" -> Warning + "error" -> Error + _ -> Error + +getCliState :: IO Bool +getCliState = argOrEnvOrBool "--cli" "HOMEPAGE_CLI" \ No newline at end of file diff --git a/app/Helpers/Utils.hs b/app/Helpers/Utils.hs index 129799e..5776661 100644 --- a/app/Helpers/Utils.hs +++ b/app/Helpers/Utils.hs @@ -22,12 +22,12 @@ linkImage label image url = [hsx| |] -items :: [String] -> Html -items values = forEach [1..length values] (\i -> [hsx| -
Where to find me:
Jægerstuen - Selma Lagerløfs Vej 300, 9220 Aalborg Øst