From a4f60df0f3b72553380bdd3ca960abf42048ed7e Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Sat, 8 Aug 2015 19:50:58 +0200 Subject: Getting the hostname and the port in config file --- src/server/Config.hs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/server/Config.hs (limited to 'src/server/Config.hs') diff --git a/src/server/Config.hs b/src/server/Config.hs new file mode 100644 index 0000000..f4144f7 --- /dev/null +++ b/src/server/Config.hs @@ -0,0 +1,31 @@ +{-# LANGUAGE FlexibleContexts #-} + +module Config + ( getConfig + , Config(..) + ) where + +import Data.ConfigFile +import Data.Text (Text) +import qualified Data.Text as T + +import Control.Monad.Trans.Error (runErrorT) +import Control.Monad.IO.Class (liftIO) +import Control.Monad (join) +import Control.Arrow (left) +import Control.Applicative (liftA2) + +data Config = Config + { hostname :: Text + , port :: Int + } deriving (Read, Eq, Show) + +getConfig :: FilePath -> IO (Either String Config) +getConfig filePath = + left show <$> (runErrorT $ do + cp <- join $ liftIO $ readfile emptyCP filePath + liftA2 + Config + (T.pack <$> get cp "DEFAULT" "hostname") + (get cp "DEFAULT" "port") + ) -- cgit v1.2.3