Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE OverloadedStrings #-}
- {-# LANGUAGE DeriveGeneric #-}
- import GHC.Generics
- import Data.Yaml
- import Control.Applicative
- data Config = Config { app :: AppConfig, db :: DbConfig } deriving (Show, Generic)
- data AppConfig = AppConfig { port :: Int } deriving (Show)
- data DbConfig = DbConfig { user :: String, password :: String, database :: String} deriving (Show, Generic)
- -- instance FromJSON Config where
- -- parseJSON (Object o) = Config <$>
- -- o .: "app" <*>
- -- o .: "db"
- -- parseJSON x = fail $ show x
- instance FromJSON Config
- instance FromJSON AppConfig where
- parseJSON (Object m) = AppConfig <$> m .:? "port" .!= 8081 -- default
- instance FromJSON DbConfig
- readConfig :: IO Config
- readConfig =
- either (error . show) id <$>
- decodeFileEither "./conf/conf.yaml"
- main :: IO ()
- main = do
- conf <- readConfig
- putStrLn $ show conf
- return ()
- --
- -- app :
- -- port :
- -- db :
- -- user : "admin"
- -- password : "pass"
- -- database : "mydb"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement