Advertisement
Guest User

Untitled

a guest
Mar 11th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. {-# LANGUAGE OverloadedStrings #-}
  2. {-# LANGUAGE DeriveGeneric #-}
  3.  
  4. import GHC.Generics
  5.  
  6. import Data.Yaml
  7. import Control.Applicative
  8.  
  9. data Config = Config { app :: AppConfig, db :: DbConfig } deriving (Show, Generic)
  10. data AppConfig = AppConfig { port :: Int } deriving (Show)
  11. data DbConfig = DbConfig { user :: String, password :: String, database :: String} deriving (Show, Generic)
  12.  
  13. -- instance FromJSON Config where
  14. -- parseJSON (Object o) = Config <$>
  15. -- o .: "app" <*>
  16. -- o .: "db"
  17. -- parseJSON x = fail $ show x
  18.  
  19. instance FromJSON Config
  20. instance FromJSON AppConfig where
  21. parseJSON (Object m) = AppConfig <$> m .:? "port" .!= 8081 -- default
  22. instance FromJSON DbConfig
  23.  
  24. readConfig :: IO Config
  25. readConfig =
  26. either (error . show) id <$>
  27. decodeFileEither "./conf/conf.yaml"
  28.  
  29. main :: IO ()
  30. main = do
  31. conf <- readConfig
  32. putStrLn $ show conf
  33. return ()
  34.  
  35. --
  36. -- app :
  37. -- port :
  38. -- db :
  39. -- user : "admin"
  40. -- password : "pass"
  41. -- database : "mydb"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement